|
| 1 | +--- |
| 2 | +title: Routing |
| 3 | +--- |
| 4 | + |
| 5 | +<Since issueNumber="MDL-81301" version="4.5" /> |
| 6 | + |
| 7 | +Moodle includes a powerful routing system based upon the [Slim Framework](https://www.slimframework.com/), and [FastRoute](https://github.com/nikic/FastRoute). |
| 8 | + |
| 9 | +Routes are defined by creating classes within the `route` L2 namespace of a component, and associating methods with the `\core\router\route` attribute. Routes have a Route _group_ identified by the L3 namespace, for example `api`. Unknown route groups are ignored. |
| 10 | + |
| 11 | +The currently supported list of route groups are: |
| 12 | + |
| 13 | +| Route Group | Namespace | URI Prefix | Purpose | |
| 14 | +| --- | --- | --- | --- | |
| 15 | +| API | `api` | `/api/rest/v2` | REST Web Services | |
| 16 | + |
| 17 | +Routes may optionally describe additional metadata including: |
| 18 | + |
| 19 | +- path parameters |
| 20 | +- optional path parameters |
| 21 | +- header parameters |
| 22 | +- HTTP method types (GET, POST, and so on) |
| 23 | +- Responses |
| 24 | +- Examples |
| 25 | + |
| 26 | +When invoked, any parameter to the route method will be resolved using a combination of [Dependency Injection](../../core/di/index.md) and resolution of path, query, and header parameters. |
| 27 | + |
| 28 | +## Using the `route` attribute |
| 29 | + |
| 30 | +When applied to a method, the `\core\router\route` attribute will create a route. Class-level attributes can also be defined and are used to define a route prefix, and some defaults, but cannot handle a route directly. |
| 31 | + |
| 32 | +The path will be combined with the URI prefix described by the route _group_ (for example `api` has a prefix of `/api/rest/v2`), and the component (for example `mod_example`) to create a fully-qualified path. |
| 33 | + |
| 34 | +Route groups are pre-defined by the Routing Subsystem and will provide a URI prefix, relevant [Middleware](https://www.php-fig.org/psr/psr-15/meta/), and some rules -- as an example the `api` route group has a route prefix of `/api/rest/v2`, and will apply a range of Route Middleware to configure CORS, perform Input and Output Sanitisation, normalise paths, and more. |
| 35 | + |
| 36 | +:::note |
| 37 | + |
| 38 | +Any unknown Route Group will be ignored. |
| 39 | + |
| 40 | +::: |
| 41 | + |
| 42 | +In the following example, the namespace of the class has: |
| 43 | + |
| 44 | +- A Level 2 namespace of `route`; and |
| 45 | +- A Level 3 namespace of `api`. |
| 46 | + |
| 47 | +This relates to the `api` route group giving it a path prefix of `/api/rest/v2`. |
| 48 | + |
| 49 | +```php title="A simple route" |
| 50 | +// mod/example/classes/route/api/example.php |
| 51 | +namespace mod_example\route\api; |
| 52 | + |
| 53 | +use core\router\schema\response\payload_response; |
| 54 | +use Psr\Http\Message\ResponseInterface; |
| 55 | +use Psr\Http\Message\ServerRequestInterface; |
| 56 | + |
| 57 | +class example { |
| 58 | + #[\core\router\route( |
| 59 | + // Resolves to https://example.com/moodle/api/rest/v2/mod_example/example |
| 60 | + path: '/example', |
| 61 | + )] |
| 62 | + public function example_route( |
| 63 | + ServerRequestInterface $request, |
| 64 | + ResponseInterface $response, |
| 65 | + ): ResponseInterface { |
| 66 | + return new payload_response( |
| 67 | + request: $request, |
| 68 | + response: $response, |
| 69 | + payload: [], |
| 70 | + ); |
| 71 | + } |
| 72 | +} |
| 73 | +``` |
0 commit comments