如何将Yii2自定义URL规则中的请求方法从GET更改为POST等?

问题描述:

I am trying to work in RESTfull web services in Yii2 using default controller. But the problem I faced is, I cannot send POST request with parameters. Below is my code:

Url Manager rule in web.php

'urlManager' => [
        'class' => 'yii\web\UrlManager',
        // Disable index.php
        'showScriptName' => false,
        // Disable r= routes
        'enablePrettyUrl' => true,
        'rules' => array(
            ['pattern' => 'api/v1/auth/payment/<id:\d+>', 'route' => 'api/v1/auth/payment'],
            '<controller:\w+>/<id:\d+>' => '<controller>/view',
            '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
            '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
        ),
    ],

AuthController.php file this is inside controller/api/v1/

namespace app\controllers\api\v1;

use app\controllers\api\v1\components\ApiFunctions;
use Yii;
use yii\web\Controller;


class AuthController extends Controller
{
    public function actionPayment()
    {
        $id = Yii::$app->getRequest()->getQueryParam('id');
        json_encode($id);
    }

}

But when I send GET request to http://{url}//api/v1/auth/payment/5 I get response as 5. But I want to get that result when sending POST or any other methods.

So how can I achieve that?

我正在尝试使用默认控制器在 Yii2 code>中的RESTfull Web服务中工作。 但我面临的问题是,我无法发送带参数的POST请求。 我的代码是: p>

web.php code> p>中的Url Manager规则

 'urlManager'=&gt;  [
'class'=&gt;  'yii \ web \ UrlManager',
 //禁用index.php 
'howScriptName'=&gt;  false,
 //禁用r = routes 
'enablePrettyUrl'=&gt; 是的,
'规则'=&gt; 数组(
 ['pattern'=&gt;'api / v1 / auth / payment /&lt; id:\ d +&gt;','route'=&gt;'api / v1 / auth / payment'],
  '&lt; controller:\ w +&gt; /&lt; id:\ d +&gt;'=&gt;'&lt; controller&gt; / view',
'&lt; controller:\ w +&gt; /&lt; action:  \ w +&gt; /&lt; id:\ d +&gt;'=&gt;'&lt; controller&gt; /&lt; action&gt;',
'&lt; controller:\ w +&gt; /&lt; action:\ w  +&gt;'=&gt;'&lt; controller&gt; /&lt; action&gt;',
),
 
,
   code>  pre> 
 
 

AuthController.php code>文件位于 controller / api / v1 / code> p>

  namespace app \ controllers \ api \ v1; 
 
use app \  controllers \ api \ v1 \ components \ ApiFunctions; 
use Yii; 
use yii \ web \ Controller; 
 
 
class AuthController extends Controller 
 {
 public function actionPayment()
 {
 $ id = Yii  :: $ app-&gt; getRequest() - &gt; getQueryParam('id'); 
 json_encode($ id); 
} 
 
} 
  code>  pre> 
 
  

但是当我将 GET code>请求发送到 http:// {url} // api / v1 / auth / payment / 5 code>时,我得到了r 响应为 5 code>。 但是我希望在发送 POST code>或任何其他方法时得到这个结果。 p>

那么我该如何实现呢? p> div>

Let me show you how i solve it for my application.

A simple application structure i have constructed for basic application setup was.

------ app

------modules

----------api

-------------modules

----------------v1

-------------------controllers

-------------------models

-------------------etc

You can simply go through the application setup first.

Then define verb filtering in every controller or sort it out by defining in a common class, as i have done here.

Then with the same rule you have defined in urlManager, you will able be to get the request query parameter.

Hope this helps.

Use

'POST <controller:\w+>s' => '<controller>/create',

See more http://www.yiiframework.com/doc-2.0/yii-web-urlmanager.html

More example from documentation

[
    'dashboard' => 'site/index',

    'POST <controller:\w+>s' => '<controller>/create',
    '<controller:\w+>s' => '<controller>/index',

    'PUT <controller:\w+>/<id:\d+>'    => '<controller>/update',
    'DELETE <controller:\w+>/<id:\d+>' => '<controller>/delete',
    '<controller:\w+>/<id:\d+>'        => '<controller>/view',
];