The simplest usage of UrlRule is to declare a rule like the following in the application configuration,
php
[
'class' => 'yii\rest\UrlRule',
'controller' => 'user',
]
The above code will create a whole set of URL rules supporting the following RESTful API endpoints:
- 'PUT,PATCH users/' => 'user/update' : update a user
- 'DELETE users/' => 'user/delete' : delete a user
- 'GET,HEAD users/' => 'user/view' : return the details/overview/options of a user
- 'POST users' => 'user/create': create a new user
- 'GET,HEAD users' => 'user/index': return a list/overview/options of users
- 'users/' => 'user/options' : process all unhandled verbs of a user
- 'users' => 'user/options': process all unhandled verbs of user collection
You may configure [[only]] and/or [[except]] to disable some of the above rules.
You may configure [[patterns]] to completely redefine your own list of rules.
You may configure [[controller]] with multiple controller IDs to generate rules for all these controllers.
For example, the following code will disable the delete rule and generate rules for both user and post controllers:
php
[
'class' => 'yii\rest\UrlRule',
'controller' => ['user', 'post'],
'except' => ['delete'],
]
The property [[controller]] is required and should represent one or multiple controller IDs.
Each controller ID should be prefixed with the module ID if the controller is within a module.
The controller ID used in the pattern will be automatically pluralized (e.g. user becomes users
as shown in the above examples).
For more details and usage information on UrlRule, see the guide article on rest routing.