## 路由名
Application routes can be assigned a name. This is useful if you want to programmatically generate a URL to a specific route with the RouteParser’s`urlFor()`method. Each routing method described above returns a`Slim\Route`object, and this object exposes a`setName()`method.
> 可以為應用程序路由分配一個名稱。
>
> 如果您希望使用路由分析器的`urlFor()`方法以編程方式生成指向特定路由的URL,這是非常有用的。
>
> 上面描述的每個路由方法都返回一個`Slim\Route`對象,這個對象公開一個`etName()`方法。
~~~php
$app->get('/hello/{name}', function ($request, $response, $args) {
echo "Hello, " . $args['name'];
})->setName('hello');
~~~
You can generate a URL for this named route with the application RouteParser’s`urlFor()`method.
您可以使用應用程序路由分析器的`urlFor()`方法為這個命名路由生成一個URL。
~~~php
$routeParser = $app->getRouteCollector()->getRouteParser();
echo $routeParser->urlFor('hello', ['name' => 'Josh'], ['example' => 'name']);
// Outputs "/hello/Josh?example=name"
~~~
The RouteParser’s`urlFor()`method accepts three arguments:
* `$routeName`The route name. A route’s name can be set via`$route->setName('name')`. Route mapping methods return an instance of`Route`so you can set the name directly after mapping the route. e.g.:`$app->get('/', function () {...})->setName('name')`
* `$data`Associative array of route pattern placeholders and replacement values.
* `$queryParams`Associative array of query parameters to be appended to the generated url.
> RouteParser的`urlFor()`方法接受三個參數:
> `$routeName`路由名稱。路由的名稱可以通過`$route->setName('name')`來設置。路由映射方法返回一個“Route”實例,這樣您就可以在映射路由后直接設置名稱。
>
> 例如:`$app->get('/', function(){…})->setName('name') `
>
> ` $data`關聯數組的路由模式占位符和替換值。
>
> `$queryParams`關聯數組的查詢參數要附加到生成的url。
- 開始
- 安裝
- 升級指南
- Web服務器
- 概念
- 生命周期
- PSR 7
- 中間件
- 依賴容器
- 實例 及通知和警告處理
- Request
- 請求方法
- 請求頭信息
- 請求主體
- 上傳的文件
- 請求幫助
- 路由對象
- Response
- 響應狀態
- 響應標頭
- 響應體
- 返回JSON
- 視圖模板
- 路由
- 創建路由
- 路由回調
- 路由策略
- 路線占位符
- 路由名
- 路由組
- 路由中間件
- 路由表達式緩存
- 容器識別解析
- 封裝中間件
- 路由的中間件
- 錯誤處理中間件
- 方法重寫的中間件
- 輸出緩沖中間件
- 內容長度中間件
- 擴展功能
- 以 / 結尾的路由模式
- 獲取當前路由
- 設置CORS
- 使用POST表單上傳文件
- 第三方組件
- slim-session
- auth
- slim-api-skeleton
- dir