yii2的restful api路由實(shí)例詳解
yii\rest\UrlRule
使用yii\rest\UrlRule來自動(dòng)映射控制器的 restful 路由,簡單快捷,缺點(diǎn)是必須得按規(guī)定好的方法名去寫業(yè)務(wù)。
映射的規(guī)則如下,當(dāng)然,你可以修改源碼為你的習(xí)慣:
public $patterns = [ 'PUT,PATCH {id}' => 'update', 'DELETE {id}' => 'delete', 'GET,HEAD {id}' => 'view', 'POST' => 'create', 'GET,HEAD' => 'index', '{id}' => 'options', '' => 'options', ];
除了被限制了HTTP動(dòng)詞對(duì)應(yīng)的方法名外,其他都很好用,比如pluralize是多么的優(yōu)雅啊,可以自動(dòng)解析單詞的復(fù)數(shù),laravel的話要一個(gè)個(gè)的去寫,反而有些不方便了
'urlManager' => [ 'enablePrettyUrl' => true, 'showScriptName' => false, 'enableStrictParsing' => true, 'rules' => [ [ 'class' => 'yii\rest\UrlRule', 'controller' => [ 'v1/user', 'v1/news', 'routeAlias' => 'v1/box' ], 'pluralize' => true ], ] ]
自定義路由
注意我路由里很刻意的用了復(fù)數(shù)模式,但很雞肋,因?yàn)橐恍﹩卧~的復(fù)數(shù)并不是簡單的加個(gè) s 就可以了。
'urlManager' => [ 'enablePrettyUrl' => true, 'showScriptName' => false, 'enableStrictParsing' => true, 'rules' => [ // 利用 module 做個(gè)版本號(hào)也是可以的 '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 ] ]
當(dāng)然,這種高度動(dòng)態(tài)的路由也可以寫的像laravel一樣半靜態(tài)。
'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',
如同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");
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
IIS6.0+PHP5.x+MySQL5.x+Zend3.0x+GD+phpMyAdmin2.8x通用安裝實(shí)例(已經(jīng)完成
IIS6.0+PHP5.x+MySQL5.x+Zend3.0x+GD+phpMyAdmin2.8x通用安裝實(shí)例(已經(jīng)完成)...2006-12-12CodeIgniter配置之a(chǎn)utoload.php自動(dòng)加載用法分析
這篇文章主要介紹了CodeIgniter配置之a(chǎn)utoload.php自動(dòng)加載用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了CodeIgniter自動(dòng)加載機(jī)制的原理與使用方法,需要的朋友可以參考下2016-01-01淺談laravel數(shù)據(jù)庫查詢返回的數(shù)據(jù)形式
今天小編就為大家分享一篇淺談laravel數(shù)據(jù)庫查詢返回的數(shù)據(jù)形式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-10-10Laravel框架控制器的middleware中間件用法分析
這篇文章主要介紹了Laravel框架控制器的middleware中間件用法,結(jié)合具體案例形式分析了Laravel框架控制器的middleware中間件相關(guān)使用步驟、操作技巧與注意事項(xiàng),需要的朋友可以參考下2019-09-09