VerbUrlManager - simple extension to handle http methods in rules

description:

Use http methods/verbs in your application rules

[b][u]how to install:

[/u][/b]

Im assuming its not the first time you are using yii’s UrlManager

To set up, download the file attached, put it in your components folder or any other folder imported in your initial configuration

and then in your configuration options (configs/main.php probably) add the following line




'urlManager'=>array(

        	'class'=>'VerbUrlManager',//this line

        	//or 'class'=>'application.extensions.verburlmanager.VerbUrlManager'



how to use :


'rules'=>array(

//rules applied to all kind of requests if a specific verbs/method rule does not match/trigger first

        '<_c:\w*>/<_a:\w+>/<id:\d+>'=>'<_c>/<_a>',

        //rules for post requests

        '@post'=>array(

                '<_c:\w+>'=>'<_c>/create',

                '<_c:\w+>/<id:\d+>'=>'<_c>/update',

        ),

        //rules for put requests

        '@put'=>array(

                '<_c:\w+>'=>'<_c>/create',

                '<_c:\w+>/<id:\d+>'=>'<_c>/update',

        ),

        '@get'=>array(

            	'<_c:\w+>'=>'<_c>/list',

                '<_c:\w+>/<id:\d+>'=>'<_c>/details',

                '<_c:\w+>/<_a:\w+>/<q:\w+>'=>'<_c>/search',

        ),

        //case insensitive

        '@DELETE'=>array(

                '<_c:\w+>'=>'error/400',

                '<_c:\w+>/<id:\d+>'=>'<_c>/delete',

        ),

        //rule is a string, which means that all requests go to a fixed route 

        '@HEAd'=>'error/501'

        )

extension link


[b]

[/b]

Thumbs up. :)

Simple and effective.

Hi,

I have followed the instructions:

  • downloaded the file

  • referenced the class in config/main.php

but can’t get it to work. I run Yii 1.1.5.

My test RESTful controller looks like:

class TestController extends Controller {

/**


 * PUT/POST requests


 */





public function actionCreate() {


    echo CJSON::encode(&quot;Create&quot;);


    Yii::app()-&gt;end();


}





public function actionUpdate() {


    echo CJSON::encode(&quot;Update&quot;);


    Yii::app()-&gt;end();


}


....

}

I use a RESTclient tool to switch between methods. If I call:

POST http://localhost/myApp/test

I’m not being routed to action create. It is kind of bypassed.

Same for POST http://localhost/myApp/test/123, not going to action update

Any idea on what I’m doing wrong? Ta

There you go, fixed, and download file updated

a little bug was causing it to ignore the routes sometimes

sorry for the incovenience

fixed bug that was causing string/fixed routes not to work properly

download file updated

Hello,

I’m a TOTAL newbee.

I’d like to to for example receive a POST request with XML inside and update the DB according to the XML.

I have a controller (let’s say “controllerA”) that parses the XML and updates the tables.

Now I’d like to use your extension for the REST part. Where to start?

It would be right to create another post for your question

Anyway, this is how I would do it

the rules you use as described in the extension link or above, which is basically define routes for the different method’s type , for example if its a post and the url is /controller/3 route it to controller/update and $_GET[‘id’] would be 3

Create a class to parse the xml the way that fits your service better

in the actions in your controller do something like :


if(ispost()){

 	$xml=MyXmlParser::parsePost($_POST);

 	//do something here with the xml, like update your tables

 	//return the correct response here

}

Obs.: The function ispost() does not exist by default, I always create it as a shortcut for


Yii::app()->getRequest()->isPostRequest

also check out this post and this post

[center][size="3"]Note: Starting from Yii 1.1.7 this functionality is included in the core framework and should be considered obsolete.[/size][/center]