yii2 的 restful api 路由实例

栏目: PHP · 发布时间: 4年前

内容简介:使用映射的规则如下,当然,你可以修改源码为你的习惯:除了被限制了

yii\rest\UrlRule

使用 yii\rest\UrlRule 来自动映射控制器的 restful 路由,简单快捷,缺点是必须得按规定好的方法名去写业务。

映射的规则如下,当然,你可以修改源码为你的习惯:

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

除了被限制了 HTTP 动词对应的方法名外,其他都很好用,比如 pluralize 是多么的优雅啊,可以自动解析单词的复数, laravel 的话要一个个的去写,反而有些不方便了

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

自定义路由

注意我路由里很刻意的用了复数模式,但很鸡肋,因为一些单词的复数并不是简单的加个 s 就可以了。

'urlManager'   => [
    'enablePrettyUrl'     => true,
    'showScriptName'      => false,
    'enableStrictParsing' => true,
    'rules'               => [
        // 利用 module 做个版本号也是可以的
        '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
    ]
]

当然,这种高度动态的路由也可以写的像 laravel 一样半静态。

'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");

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

500 Lines or Less

500 Lines or Less

Amy Brown、Michael DiBernardo / 2016-6-28 / USD 35.00

This book provides you with the chance to study how 26 experienced programmers think when they are building something new. The programs you will read about in this book were all written from scratch t......一起来看看 《500 Lines or Less》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具