企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## Route placeholders路线占位符 Each routing method described above accepts a URL pattern that is matched against the current HTTP request URI. Route patterns may use named placeholders to dynamically match HTTP request URI segments. > 上面描述的每个路由方法都接受一个与当前HTTP请求URI匹配的URL模式。路由模式可以使用命名占位符来动态匹配HTTP请求URI段。 ### Format格式化 A route pattern placeholder starts with a `{` , followed by the placeholder name, ending with a `}` . This is an example placeholder named `name` : > 路由模式占位符以`{`开头,后跟占位符名称,以`}`结尾。这是一个名为`name`的占位符示例: ~~~php $app->get('/hello/{name}', function (Request $request, Response $response, $args) { $name = $args['name']; echo "Hello, $name"; }); ~~~ ### Optional segments可选部分 To make a section optional, simply wrap in square brackets: > 要使一个部分可选,只需用方括号括起来: ~~~php $app->get('/users[/{id}]', function ($request, $response, $args) { // responds to both `/users` and `/users/123` // but not to `/users/` }); ~~~ Multiple optional parameters are supported by nesting: > 嵌套支持多个可选参数: ~~~php $app->get('/news[/{year}[/{month}]]', function ($request, $response, $args) { // reponds to `/news`, `/news/2016` and `/news/2016/03` }); ~~~ For “Unlimited” optional parameters, you can do this: 对于`传入`可选参数,你可以这样做: ~~~php $app->get('/news[/{params:.*}]', function ($request, $response, $args) { // $params is an array of all the optional segments $params = explode('/', $args['params']); }); ~~~ In this example, a URI of`/news/2016/03/20`would result in the`$params`array containing three elements:`['2016', '03', '20']`. > 在本例中,URI`/news/2016/03/20`将导致`$params`数组包含三个元素:` ['2016','03','20']` ### Regular expression matching正则表达式匹配 By default the placeholders are written inside`{}`and can accept any values. However, placeholders can also require the HTTP request URI to match a particular regular expression. If the current HTTP request URI does not match a placeholder regular expression, the route is not invoked. This is an example placeholder named`id`that requires one or more digits. > 默认情况下占位符写在`{}`里面,可以接受任何值。但是,占位符也可能需要HTTP请求URI来匹配特定的正则表达式。如果当前HTTP请求URI不匹配占位符正则表达式,则不会调用路由。这是一个名为`id`的占位符示例,它需要一个或多个数字。 ~~~php $app->get('/users/{id:[0-9]+}', function ($request, $response, $args) { // Find user identified by $args['id'] }); ~~~