Detailed explanation of restful api routing instance of yii2

  • 2021-12-11 07:09:12
  • OfStack

yii\rest\UrlRule

Using yii\ rest\ UrlRule to automatically map the restful route of the controller is simple and fast, but the disadvantage is that the service must be written according to the specified method name.

The mapping rules are as follows. Of course, you can modify the source code for your habits:


public $patterns = [
  'PUT,PATCH {id}' => 'update',
  'DELETE {id}' => 'delete',
  'GET,HEAD {id}' => 'view',
  'POST' => 'create',
  'GET,HEAD' => 'index',
  '{id}' => 'options',
  '' => 'options',
];

In addition to the limited HTTP verb corresponding method name, others are very easy to use, such as pluralize is how elegant ah, can automatically analyze the plural of words, laravel to write one by one, but some inconvenient


'urlManager'  => [
  'enablePrettyUrl'   => true,
  'showScriptName'   => false,
  'enableStrictParsing' => true,
  'rules'        => [
    [
      'class'   => 'yii\rest\UrlRule',
      'controller' => [
        'v1/user',
        'v1/news',
        'routeAlias' => 'v1/box'
      ],
      'pluralize' => true
    ],
  ]
]

Custom routing

Note that I used the plural mode deliberately in the route, but it is very chicken ribs, because the plural number of 1 words is not simply to add an s.


'urlManager'  => [
  'enablePrettyUrl'   => true,
  'showScriptName'   => false,
  'enableStrictParsing' => true,
  'rules'        => [
    //  Utilization  module  It is also ok to make a version number 
    'GET <module:(v1|v2)>/<controller:\w+>s'         => '<module>/<controller>/index',
    'GET <module:(v1|v2)>/<controller:\w+>s/<uid:\d+>'    => '<module>/<controller>/view',
    'POST <module:(v1|v2)>/<controller:\w+>s'        => '<module>/<controller>/create',
    'PUT,PATCH <module:(v1|v2)>/<controller:\w+>s/<uid:\d+>' => '<module>/<controller>/update',
    'DELETE <module:(v1|v2)>/<controller:\w+>s/<uid:\d+>'  => '<module>/<controller>/delete',
    'OPTIONS <module:(v1|v2)>/<controller:\w+>s'       => '<module>/<controller>/options',

    '<controller:\w+>/<action:\w+>'       => '<controller>/<action>',// normal
    '<module:\w+>/<controller:\w+>/<action:\w+>' => '<module>/<controller>/<action>',// module
    '/'                     => 'site/default',// default route
  ]
]

Of course, this highly dynamic route can also be written as semi-static like laravel1.


'GET v1/children'         => 'v1/child/index',
'GET v1/children/<uid:\d+>'    => 'v1/child/view',
'POST v1/children'        => 'v1/child/create',
'PUT,PATCH v1/children/<uid:\d+>' => 'v1/child/update',
'DELETE v1/children/<uid:\d+>'  => 'v1/child/delete',
'OPTIONS v1/children'       => 'v1/child/options',

As in laravel


Route::get("/v1/children", "ChildController@index");
Route::post("/v1/children", "ChildController@create");
Route::put("/v1/children/{uid}", "ChildController@update");
Route::patch("/v1/children/{uid}", "ChildController@update");
Route::delete("/v1/children/{uid}", "ChildController@delete");
Route::options("/v1/children", "ChildController@options");

Related articles: