YII URLManager for RESTfull API

Trying to work with RESTfull API with yii (being the first project using yii)

Problem getting URLManager for proper call routing:

'urlManager' => array( 'urlFormat' => 'path', 'showScriptName' => false, 'caseSensitive' => false, 'rules' => array( 'api/<controller>' => array('api/<controller>/list', 'verb' => 'GET'), 'api/<controller>' => array('api/<controller>/create', 'verb' => 'POST'), ), ), 

I tried to work with this (this is not a complete fragment, I had dispatchers for PUT / DELETE, etc. But this did not work ... Being desperate, I even tried something simple:

  'urlManager' => array( 'urlFormat' => 'path', 'showScriptName' => false, 'caseSensitive' => false, 'rules' => array( 'tezt' => array('landing/beta', 'verb' => 'GET'), 'tezt' => array('landing', 'verb' => 'POST'), ), ), 

whenever I delete one of the rules, it works, but when I insert both rules, neither of them works, I get an exception.CHttpException.404 exception

  exception 'CHttpException' with message 'Unable to resolve the request "tezt".' in /yii-1.1.10/web/CWebApplication.php:280 

Again I hit my head about it for 2 days. They probably looked through all the URLManager samples and tutorials on the Internet (although they could not find a simple and complete explanation of the rules). But there is no joy.

Am I doing something wrong? Perhaps this is my box installation?

+4
source share
3 answers

I tried this and it worked:

 'rules'=>array( //API URLs array('api/<controller>/index', 'pattern'=>'api/<controller:\w+>', 'verb'=>'GET'), array('api/<controller>/create', 'pattern'=>'api/<controller:\w+>', 'verb'=>'POST'), array('api/<controller>/view', 'pattern'=>'api/<controller:\w+>/<id:\d+>', 'verb'=>'GET'), array('api/<controller>/update', 'pattern'=>'api/<controller:\w+>/<id:\d+>', 'verb'=>'PUT, POST'), array('api/<controller>/delete', 'pattern'=>'api/<controller:\w+>/<id:\d+>', 'verb'=>'DELETE'), //Other URLs '<controller:\w+>/<id:\d+>'=>'<controller>/view', '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>', '<controller:\w+>/<action:\w+>'=>'<controller>/<action>', ), 

"api" is a module configured in the modules section as

 'api'=>array('defaultController' => 'default',), 

In the REST client, you must specify the name of the controller, even for the default controller.

I am using Yii 1.1.10, but I think that Yii has supported RESTful URLs since version 1.1.7.

+6
source

try it

 'api/<controller:\w+>' => array('api/<controller>/list', 'verb' => 'GET'), 

is api module?

0
source

For anyone who stumbled upon this, this did not work because the rules were declared using the same keys, so the last rule overrides the previous one.

In the future, declare the template in the rules configuration array:

 array( 'route', 'pattern' =>'somePattern', 'verb' =>'...', ), array( 'another/route', 'pattern' =>'anotherPattern', 'verb' =>'...', ), 
0
source

Source: https://habr.com/ru/post/1400787/


All Articles