您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
webman-路由-路由参数 - 更多例子
发布时间:2022-01-19 22:30:33编辑:雪饮阅读()
路由配置于config/route.php:
仅匹配数字类型参数
<?php
/**
* This file is part of webman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<walkor@workerman.net>
* @copyright walkor<walkor@workerman.net>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
use Webman\Route;
//仅匹配数字参数
Route::any('/user/{id:\d+}', function ($request, $id) {
return response($id);
});
实例被请求:
[root@localhost ~]# elinks http://127.0.0.1:8787/user/1 --dump
1
[root@localhost ~]# elinks http://127.0.0.1:8787/user/aa --dump
404 Not Found
--------------------------------------------------------------------------
[1]webman
References
Visible links
1. https://www.workerman.net/
//不限制匹配的参数
Route::any('/user/{name}', function ($request, $name) {
return response($name);
});
实例被请求:
[root@localhost ~]# elinks http://127.0.0.1:8787/user/aa --dump
aa
[root@localhost ~]# elinks http://127.0.0.1:8787/user/1 --dump
1
// 匹配 参数可以带,也可以不带,甚至可以不用带'/',不带参数是使用默认参数值tom,若带'/'则必须有参数值
Route::any('/user[/{name}]', function ($request, $name = null) {
return response($name ?? 'tom');
});
实例被请求:
[root@localhost ~]# elinks http://127.0.0.1:8787/user --dump
tom
[root@localhost ~]# elinks http://127.0.0.1:8787/user/ --dump
404 Not Found
--------------------------------------------------------------------------
[1]webman
References
Visible links
1. https://www.workerman.net/
[root@localhost ~]# elinks http://127.0.0.1:8787/user/a --dump
a
[root@localhost ~]# elinks http://127.0.0.1:8787/user/2 --dump
2
关键字词:webman,路由,参数,更多,例子
上一篇:webman-路由-处理404
下一篇:webman-路由-路由参数