## 創建路由
You can define application routes using proxy methods on the`Slim\App`instance. The Slim Framework provides methods for the most popular HTTP methods.
> 您可以在`slim \App`實例上使用代理方法定義應用程序路由。Slim框架為最流行的HTTP方法提供了方法。
### GET Route
You can add a route that handles only`GET`HTTP requests with the Slim application’s`get()`method. It accepts two arguments:
1. The route pattern (with optional named placeholders)
2. The route callback
> 您可以使用Slim應用程序的`get()`方法添加一個只處理`get`http請求的路由。它接受兩個參數:
> 1. 路由模式(帶有可選的命名占位符)
> 2. 路由回調
~~~php
$app->get('/books/{id}', function ($request, $response, $args) {
// Show book identified by $args['id']
});
~~~
### POST Route
You can add a route that handles only`POST`HTTP requests with the Slim application’s`post()`method. It accepts two arguments:
1. The route pattern (with optional named placeholders)
2. The route callback
> 您可以使用Slim應用程序的`post()`方法添加一個只處理`post`http請求的路由。它接受兩個參數:
> 1. 路由模式(帶有可選的命名占位符)
> 2. 路由回調
~~~php
$app->post('/books', function ($request, $response, $args) {
// Create new book
});
~~~
### PUT Route
You can add a route that handles only`PUT`HTTP requests with the Slim application’s`put()`method. It accepts two arguments:
1. The route pattern (with optional named placeholders)
2. The route callback
> 您可以使用Slim應用程序的`put()`方法添加一個僅處理`put` http請求的路由。它接受兩個參數:
>
> 1. 路由模式(帶有可選的命名占位符)
>
> 2. 路由回調
~~~php
$app->put('/books/{id}', function ($request, $response, $args) {
// Update book identified by $args['id']
});
~~~
### DELETE Route
You can add a route that handles only`DELETE`HTTP requests with the Slim application’s`delete()`method. It accepts two arguments:
1. The route pattern (with optional named placeholders)
2. The route callback
> 您可以使用Slim應用程序的`delete()`方法添加一個只處理`delete`http請求的路由。它接受兩個參數:
>
> 1. 路由模式(帶有可選的命名占位符)
>
> 2. 路由回調
~~~php
$app->delete('/books/{id}', function ($request, $response, $args) {
// Delete book identified by $args['id']
});
~~~
### OPTIONS Route
You can add a route that handles only`OPTIONS`HTTP requests with the Slim application’s`options()`method. It accepts two arguments:
1. The route pattern (with optional named placeholders)
2. The route callback
> 您可以使用Slim應用程序的`options()`方法添加一個只處理`options`http請求的路由。它接受兩個參數:
>
> 1. 路由模式(帶有可選的命名占位符)
>
> 2. 路由回調
~~~php
$app->options('/books/{id}', function ($request, $response, $args) {
// Return response headers
});
~~~
### PATCH Route
You can add a route that handles only`PATCH`HTTP requests with the Slim application’s`patch()`method. It accepts two arguments:
1. The route pattern (with optional named placeholders)
2. The route callback
> 您可以使用Slim應用程序的`patch()`方法添加一個只處理`patch`http請求的路由。它接受兩個參數:
> 1. 路由模式(帶有可選的命名占位符)
> 2. 路由回調
~~~php
$app->patch('/books/{id}', function ($request, $response, $args) {
// Apply changes to book identified by $args['id']
});
~~~
### Any Route
You can add a route that handles all HTTP request methods with the Slim application’s`any()`method. It accepts two arguments:
1. The route pattern (with optional named placeholders)
2. The route callback
> 您可以使用Slim應用程序的`any()`方法添加一個處理所有HTTP請求方法的路由。它接受兩個參數:
> 1. 路由模式(帶有可選的命名占位符)
> 2. 路由回調
~~~php
$app->any('/books/[{id}]', function ($request, $response, $args) {
// Apply changes to books or book identified by $args['id'] if specified.
// To check which method is used: $request->getMethod();
});
~~~
Note that the second parameter is a callback. You could specify a Class which implementes the`__invoke()`method instead of a Closure. You can then do the mapping somewhere else:
> 注意第二個參數是一個回調。您可以指定一個類來實現`invoke()`方法,而不是一個閉包。然后你可以在其他地方做映射:
~~~php
$app->any('/user', 'MyRestfulController');
~~~
### Custom Route
You can add a route that handles multiple HTTP request methods with the Slim application’s`map()`method. It accepts three arguments:
1. Array of HTTP methods
2. The route pattern (with optional named placeholders)
3. The route callback
> 您可以使用Slim應用程序的`map()`方法添加一個處理多個HTTP請求方法的路由。它接受三個參數:
>
> 1. HTTP方法數組
>
> 2. 路由模式(帶有可選的命名占位符)
>
> 3.路由回調
~~~php
$app->map(['GET', 'POST'], '/books', function ($request, $response, $args) {
// Create new book or list all books
});
~~~
- 開始
- 安裝
- 升級指南
- Web服務器
- 概念
- 生命周期
- PSR 7
- 中間件
- 依賴容器
- 實例 及通知和警告處理
- Request
- 請求方法
- 請求頭信息
- 請求主體
- 上傳的文件
- 請求幫助
- 路由對象
- Response
- 響應狀態
- 響應標頭
- 響應體
- 返回JSON
- 視圖模板
- 路由
- 創建路由
- 路由回調
- 路由策略
- 路線占位符
- 路由名
- 路由組
- 路由中間件
- 路由表達式緩存
- 容器識別解析
- 封裝中間件
- 路由的中間件
- 錯誤處理中間件
- 方法重寫的中間件
- 輸出緩沖中間件
- 內容長度中間件
- 擴展功能
- 以 / 結尾的路由模式
- 獲取當前路由
- 設置CORS
- 使用POST表單上傳文件
- 第三方組件
- slim-session
- auth
- slim-api-skeleton
- dir