TP5 Restful路由设置

本文深入探讨了TP5框架中路由设置的问题,特别是Restful资源路由的实现方式,并对比了不同路由注册方法的效果,最终揭示了资源路由能正确映射到不同URL及控制器方法的原因。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

TP5 路由设置

TP5路由注册

按照TP5官方文档Restful资源路由可以这样简化编写:

Route::get('api/:ver/test', 'api/:ver.test/index');

Route::get('api/:ver/test/:number', 'api/:ver.test/read');

表示我们通过下面这两个url:
http://www.abc.com/api/v1/test
http://www.abc.com/api/v1/test/123
可以分别访问控制器index和read这两个方法。但是经过实验,这两个url只会访问index方法。
这不科学,继续研究,我通过另一种方法注册路由:

Route::resource('api/:ver/test', 'api/:ver.test');

自动生成7个路由规则,如下:

标识请求类型生成路由规则对应操作方法(默认)
indexGETblogindex
createGETblog/createcreate
savePOSTblogsave
readGETblog/:idread
editGETblog/:id/editedit
updatePUTblog/:idupdate
deleteDELETEblog/:iddelete

然而这次可以正确的通过不同url访问不同的控制器方法,Why?
先看看Route::resource方式是怎么注册路由的。
定位到Route.php文件resource方法:

public static function resource($rule, $route = '', $option = [], $pattern = [])
    {
        if (is_array($rule)) {
            foreach ($rule as $key => $val) {
                if (is_array($val)) {
                    list($val, $option, $pattern) = array_pad($val, 3, []);
                }
                self::resource($key, $val, $option, $pattern);
            }
        } else {
            if (strpos($rule, '.')) {
                // 注册嵌套资源路由
                $array = explode('.', $rule);
                $last  = array_pop($array);
                $item  = [];
                foreach ($array as $val) {
                    $item[] = $val . '/:' . (isset($option['var'][$val]) ? $option['var'][$val] : $val . '_id');
                }
                $rule = implode('/', $item) . '/' . $last;
            }
            // 注册资源路由
            foreach (self::$rest as $key => $val) {
                if ((isset($option['only']) && !in_array($key, $option['only']))
                    || (isset($option['except']) && in_array($key, $option['except']))) {
                    continue;
                }
                if (isset($last) && strpos($val[1], ':id') && isset($option['var'][$last])) {
                    $val[1] = str_replace(':id', ':' . $option['var'][$last], $val[1]);
                } elseif (strpos($val[1], ':id') && isset($option['var'][$rule])) {
                    $val[1] = str_replace(':id', ':' . $option['var'][$rule], $val[1]);
                }
                $item           = ltrim($rule . $val[1], '/');
                $option['rest'] = $key;
                self::rule($item . '$', $route . '/' . $val[2], $val[0], $option, $pattern);
            }
        }
    }

查看最后一行代码发现resource方法内部给路由规则后面加了个$符号。
修改

Route::get('api/:ver/test$', 'api/:ver.test/index');

Route::get('api/:ver/test/:number$', 'api/:ver.test/read');

发现一切正常了,两个方法可以正常访问到。
但是在文档没有看到使用$的说明,所以继续找正确的方法,最后在setRule方法发现了端倪:
这里写图片描述
complete_match这个选项表示是否完整匹配路由,然后我查看了项目的配置文件:

    // 路由使用完整匹配
    'route_complete_match'   => false,

项目配置并没有使用路由完整性匹配,所以只要找到了第一个匹配的路由就会进入该方法,而resouce方法使用神奇的内部符号$,相当于针对该路由设置了完整性匹配规则。所以正确的使用办法应该是:
修改'route_complete_match' => true,或者给路由添加['complete_match' => true]选项

Route::get('api/:ver/test$', 'api/:ver.test/index', ['complete_match' => true]);

Route::get('api/:ver/test/:number$', 'api/:ver.test/read', ['complete_match' => true]);

一切正常~
未完待续~~~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值