企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## 创建路由 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 }); ~~~