Routing requests to module controllers

HI Im looking for some information on the best way to route requests to a custom module in the Yii framework.

I am implementing a RestFul Api for a project and was hoping that there was some way i could simply route all requests to api/ to the module where the rest api lives. Even better than that would be away to someone how route the api requests to a custom UrlManager class that extends CUrlManager in the module that then deals with the routes. so a request to mydomain/api/user/model would actually be deferred and handled by a UrlManager component in the module and other requests ie mydoamin.com/client/create would simply be handled by the normal yii applicaton. As far as i can tell this isnt possible!!

So i will settle for definig a url manager class in my config that catches the api routes like so


    

class UrlManager extends CUrlManager

    {

    	protected function processRules()

    	{

    		if(!isset($_GET['r']))

    		{			

    			$this->setUrlFormat('path');

    			$this->showScriptName=false;

    			$this->rules=array(

    				

    				//Api Rest Patterns

    				array('api/list', 'pattern'=>'^api/user/<model:\w+>', 'verb'=>'GET'),

    				array('api/view', 'pattern'=>'^api/user/<model:\w+>/<id:\d+>', 'verb'=>'GET'),

    				array('api/update', 'pattern'=>'^api/user/<model:\w+>/<id:\d+>', 'verb'=>'PUT'),

    		        array('api/delete', 'pattern'=>'^api/user/<model:\w+>/<id:\d+>', 'verb'=>'DELETE'),

    		        array('api/add', 'pattern'=>'^api/user/<model:\w+>', 'verb'=>'POST'),

    				array('api/test', 'pattern'=>'^api/user/test/<model:\w+>'),

    				array('api/login', 'pattern'=>'^api/user/<model:\w+>/login'),

    				array('api/logout', 'pattern'=>'^api/user/<model:\w+>/logout'),

    				

    				

    		        // Other controllers

    				'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',

    		        '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',                

    				'<action:\w+>'=>'site/<action>',

    			);

    		}

    		return parent::processRules();

    	}

    }



My question is then, how do i route the request to a Controller in the Module? ie module/RestApi/controller/UserApiController.php


array('ModuleController/action' , pattern=>'api/user/<model>' , 'verb'=>'GET)

I thought about this


'controllerMap' => array(

        'api'=>'application.modules.RestApi.components.ApiManager',

    ),

But im pretty certain i need two points of access, one for dealing with admin tasks on the api and one for users, so currently my controller structure looks like:

  • RestApiController

  • UserApiController (extends RestApiController)

  • AdminApiController (extends RestApiController)

So if there was a way to dynamically route the actions to the child controllers that might work? Hope i haven’t been to confusing here really hope so of you Yii Masters can help with this problem!!

Thanks in advance

Anyone got any ideas on this

I have same issue

I have a module : modules/backend

which has defaultController ( I don’t want to change )

I can access

/backend (defaultController/index action)

but could not route other actions e.g view/edit/delete


'urlManager'=>array(

			'urlFormat'=>'path',		

  	                'showScriptName'=>false,

			'rules'=>array(

			

				'<controller:\w+>/<id:\d+>'=>'<controller>/view',

				'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',				

				'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',

			),

		),

Routing to module "Cms" into its controller "ContentController" can be defined like this:

… in config/main.php




...

'urlManager'=>array(

   'urlFormat'=>'path',

   'rules'=>array(

	'content/<action>/*'=>'cms/content/<action>',

   ),

)

...



This will convert all URL links from:




$url = Yii::app()->createUrl('cms/content/delete', array('key1' => value1, ..))



into




/content/delete/key1/value1/key2/value2



And also reversely, URL links:




http://mysite.com/content/delete/key1/value1/key2/value2



will be routed to module "Cms" and its ContentController::deleteAction().

You cannot refer directly to a model via URL link.

Lubos