# Routing By default, routing uses the [nikic/fast-route](https://github.com/nikic/FastRoute) package. The [hyperf/http-server](https://github.com/hyperf/http-server) component is responsible for connecting to the `Hyperf` server while `RPC` routing is implemented by [hyperf/rpc-server](https://github.com/hyperf/rpc-server) component. ## HTTP routing ### Define routing via configuration file Under the [hyperf-skeleton](https://github.com/hyperf/hyperf-skeleton) skeleton, all routing definitions are defined in the `config/routes.php` file by default. `Hyperf` also supports `annotation routing`, which is the recommended method, especially when there are a lot of routes. #### Defining routes using closures Only a URI and a closure (Closure) are needed to construct a basic route: ```php When using `#[AutoController]` annotation, `use Hyperf\HttpServer\Annotation\AutoController;` namespace is required. Pascal case controller names will be converted to snake_case automatically. The following is an example of the correspondence between the controller, annotation and the resulting route: | Controller | Annotation | Route URI | |:----------------:|:----------------------------------:|:--------------:| | MyDataController | #[AutoController] | /my_data/index | | MydataController | #[AutoController] | /mydata/index | | MyDataController | #[AutoController(prefix: "/data")] | /data/index | ```php input('id', 1); return (string)$id; } } ``` #### Controller annotation `#[Controller]` exists to meet more detailed routing definition requirements. The use of the `#[Controller]` annotation is used to indicate that the current class is a `controller` class, and the `#[RequestMapping]` annotation is required to update the detailed definition of request method and URI. We also provide a variety of quick and convenient `mapping` annotations, such as `#[GetMapping]`, `#[PostMapping]`, `#[PutMapping]`, `#[PatchMapping]` and `#[DeleteMapping]`, each corresponding with a matching request method. - When using `#[Controller]` annotation, `use Hyperf\HttpServer\Annotation\Controller` namespace is required. - When using `#[RequestMapping]` annotation, `use Hyperf\HttpServer\Annotation\RequestMapping` namespace is required. - When using `#[GetMapping]` annotation, `use Hyperf\HttpServer\Annotation\GetMapping` namespace is required. - When using `#[PostMapping]` annotation, `use Hyperf\HttpServer\Annotation\PostMapping` namespace is required. - When using `#[PutMapping]` annotation, `use Hyperf\HttpServer\Annotation\PutMapping` namespace is required. - When using `#[PatchMapping]` annotation, `use Hyperf\HttpServer\Annotation\PatchMapping` namespace is required. - When using `#[DeleteMapping]` annotation, `use Hyperf\HttpServer\Annotation\DeleteMapping` namespace is required. ```php input('id', 1); return (string)$id; } } ``` #### Annotation parameters Both `#[Controller]` and `#[AutoController]` provide two parameters, `prefix` and `server`. `prefix` represents the URI prefix for all methods under the controller, the default is the lowercase of the class name. For example, in the case of `UserController`, the `prefix` defaults to `user`, so if the controller method is `index`, then the final route is `/user/index`. It should be noted that the `prefix` is not always used: when the `path` of a method in a class starts with `/`, it means that the path is defined as an absolute `URI` and the value of `prefix` will be ignored. At the same time, if the `prefix` attribute is not set, then the part after `\\Controller\\` in the controller class namespace will be used as the route prefix in SnakeCase style. `server` indicates which server the route is defined for. Since `Hyperf` supports starting multiple servers at the same time, there may be multiple HTTP servers running at the same time. Therefore defining the `server` parameter can be used to distinguish which server the route is defined for. The default is `http`. ### Route parameters > Given route parameters must be consistent with the controller parameter key name and type, otherwise the controller cannot accept the relevant parameters ```php Router::get('/user/{id}', 'App\Controller\UserController::info'); ``` Access route parameter via controller method injection. ```php public function info(int $id) { $user = User::find($id); return $user->toArray(); } ``` Access route parameter via request object. ```php public function index(RequestInterface $request) { // If it exists, it will return, if it does not exist, it will return the default value null $id = $request->route('id'); // If it exists, it returns, if it doesn't exist, it returns the default value 0 $id = $request->route('id', 0); } ``` #### Required parameters We can define required route parameters using `{}`. For example, `/user/{id}` declares that `id` is a required parameter. #### Optional parameters Sometimes you may want a route parameter to be optional. In this case, you can use `[]` to declare the parameter inside the brackets as an optional parameter, such as `/user/[{id}]`. #### Get routing information If the devtool component is installed, you can use the `php bin/hyperf.php describe:routes` command to get the routing list information. You can also provide the path option, which is convenient for obtaining the information of a single route, for example: `php bin/hyperf.php describe:routes --path=/foo/bar`. ## HTTP exceptions When the route fails to match the route, such as `route not found (404)`, `request method not allowed (405)` and other HTTP exceptions, Hyperf will uniformly throw an exception that inherits the `Hyperf\HttpMessage\Exception\HttpException` class. You need to manage these exceptions through the `ExceptionHandler` mechanism and do the corresponding response processing. By default, you can directly use the `Hyperf\HttpServer\Exception\Handler\HttpExceptionHandler` provided by the component for exception capture and processing. Not that you need to configure this exception handler in the `config/autoload/exceptions.php` configuration file and ensure that the sequence link between multiple exception handlers is correct. When you need to customize the response to HTTP exceptions such as `route not found (404)` and `request method not allowed (405)`, you can directly implement your own exception handling based on the code of `HttpExceptionHandler` And configure your own exception handler. For the logic and usage instructions of the exception handler, please refer to [Exception Handling](en/exception-handler.md).