Yii Framework Forum: VerbUrlManager - simple extension to handle http methods in rules - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

VerbUrlManager - simple extension to handle http methods in rules useful for rest applications Rate Topic: -----

#1 User is offline   Gustavo 

  • Master Member
  • Yii
  • Group: Moderators
  • Posts: 916
  • Joined: 27-July 10
  • Location:Curitiba - Brasil

Posted 18 December 2010 - 06:17 PM

description:

Use http methods/verbs in your application rules

how to install:

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


----------------------

Quote

Note: Starting from Yii 1.1.7 this functionality is included in the core framework and should be considered obsolete.

This post has been edited by Gustavo: 21 January 2011 - 11:19 PM

--
Extensions:
translate modue - module to handle translations
multiActiveRecord - db selection in models
redisCache - redis cache component
mpCpanel - interact with cpanel api
mUploadify - use uploadify uploader in your application

Gustavo Salomé Silva
2

#2 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,697
  • Joined: 10-October 10
  • Location:Denmark

Posted 18 December 2010 - 06:21 PM

Thumbs up. :)
Simple and effective.
"Less noise - more signal"
0

#3 User is offline   vincentc8 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 20-December 10

Posted 20 December 2010 - 09:24 PM

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("Create");
Yii::app()->end();
}

public function actionUpdate() {
echo CJSON::encode("Update");
Yii::app()->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
0

#4 User is offline   Gustavo 

  • Master Member
  • Yii
  • Group: Moderators
  • Posts: 916
  • Joined: 27-July 10
  • Location:Curitiba - Brasil

Posted 20 December 2010 - 09:40 PM

There you go, fixed, and download file updated
a little bug was causing it to ignore the routes sometimes
sorry for the incovenience
--
Extensions:
translate modue - module to handle translations
multiActiveRecord - db selection in models
redisCache - redis cache component
mpCpanel - interact with cpanel api
mUploadify - use uploadify uploader in your application

Gustavo Salomé Silva
0

#5 User is offline   Gustavo 

  • Master Member
  • Yii
  • Group: Moderators
  • Posts: 916
  • Joined: 27-July 10
  • Location:Curitiba - Brasil

Posted 21 December 2010 - 09:49 AM

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

download file updated



--
Extensions:
translate modue - module to handle translations
multiActiveRecord - db selection in models
redisCache - redis cache component
mpCpanel - interact with cpanel api
mUploadify - use uploadify uploader in your application

Gustavo Salomé Silva
0

#6 User is offline   toki 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 12-January 11

Posted 19 January 2011 - 09:17 AM

View PostGustavo, on 21 December 2010 - 09:49 AM, said:

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?
0

#7 User is offline   Gustavo 

  • Master Member
  • Yii
  • Group: Moderators
  • Posts: 916
  • Joined: 27-July 10
  • Location:Curitiba - Brasil

Posted 19 January 2011 - 03:21 PM

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

This post has been edited by Gustavo: 20 January 2011 - 08:58 AM

--
Extensions:
translate modue - module to handle translations
multiActiveRecord - db selection in models
redisCache - redis cache component
mpCpanel - interact with cpanel api
mUploadify - use uploadify uploader in your application

Gustavo Salomé Silva
0

#8 User is offline   Gustavo 

  • Master Member
  • Yii
  • Group: Moderators
  • Posts: 916
  • Joined: 27-July 10
  • Location:Curitiba - Brasil

Posted 23 January 2011 - 04:54 AM

Note: Starting from Yii 1.1.7 this functionality is included in the core framework and should be considered obsolete.
--
Extensions:
translate modue - module to handle translations
multiActiveRecord - db selection in models
redisCache - redis cache component
mpCpanel - interact with cpanel api
mUploadify - use uploadify uploader in your application

Gustavo Salomé Silva
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users