JSON Router
Less than 1 minute
JSON Router
The JSON router allows you to define routes in a JSON file, providing a flexible and dynamic way to configure routes. This method is useful for applications requiring dynamic route definitions from external configurations.
routes.json
Define routes in a routes.json file in the application config directory. The file contains an array of route objects, each specifying the following properties:
route: The URL pattern.controller: The controller class name.action: The controller method name.method(optional): The HTTP method (GET, POST, PUT, DELETE).responseType(optional): The expected response type.
Example routes.json file:
[
{
"route": "/api/v1/product/list",
"controller": "Application\\Controller\\Product",
"action": "list",
"method": "GET"
},
{
"route": "/api/v1/product/{int:id}",
"controller": "Application\\Controller\\Product",
"action": "getProduct",
"method": "GET"
}
]Loading Routes
To load the JSON routes, add the following configuration to the app.php file:
<?php
return [
'development' => [
'router' => [
'type' => 'json',
'file' => 'routes.json',
],
],
];The file key specifies the path to the JSON file containing the route definitions.
Tips
The JSON router supports route parameters and response types, similar to the file router.
For route parameters, response types, and middleware, see the routing overview.