## 创建路由
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