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 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()








    				//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






    		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(



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











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

… in config/main.php









This will convert all URL links from:

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



And also reversely, URL links:

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

You cannot refer directly to a model via URL link.