"Admin module" taking over

Hi

I have an “admin” module and I want it to serve “dynamic controllers”, i.e. to provide a default behaviour for controllers which don’t really exist (“virtual controllers”).

I’ve invented a lightweight messaging mechanism for loose communication between modules. I’d like to use it such that when e.g.


?r=admin/users/index

is requested, it will call the "virtual controller" "UserController" of AdminModule, which would, by default, use this messaging mechanism to notify the real module "UsersModule" it can answer to the request.

I thought about simulating this behaviour in AdminModule::init(), but at that point I have no way of deciding whether the action can be processed by a real controller or not, or at least I don’t know how to do it.

This is because of the way Yii works: bottom-up, the controller is the one that renders the view AND the application (or the module’s, if it exists) layout, for example. I don’t think the module even has a word to say about handling a given controller+action or not.

To recap, I’m looking for a kind of CWebModule::missingController($controllerId,$actionId), just like CController::missingAction($actionId), or a workaround to simulate that.

That would possibly be in CWebModule::init() or somewhere where I can find out

  1. whether the controller actually exists, in which case its his job to handle it

  2. the $actionID and $controllerID

  3. whether the module $controllerID exists (I didn’t type it wrong, in r=admin/users/index, “users” is the actual module, as specified in the application’s config).

CWebModule::beforeControllerAction()

If you look carefully, the function signature is:


public boolean beforeControllerAction(CController $controller, CAction  $action)

Which lets me (rightfully) suppose this handles only existing controllers/actions, since the parameters are supposed to be objects created by CWebApplication::createController(), not action/controller IDs.

I’ve edited my question to clarify things.

Okay I see.

A workaround that comes into my mind is to define catchAllRequest. In the catch-all-controller you could check if a controller exists for the given route and then run it - if it doesn’t exist you do whatever is needed.

See current implementation of CWebApplication::processRequest():




	public function processRequest()

	{

		if(is_array($this->catchAllRequest) && isset($this->catchAllRequest[0]))

		{

			$route=$this->catchAllRequest[0];

			foreach(array_splice($this->catchAllRequest,1) as $name=>$value)

				$_GET[$name]=$value;

		}

		else

			$route=$this->getUrlManager()->parseUrl($this->getRequest());

		$this->runController($route);

	}



So, maybe this works in your catch-all-controller:




class CatchAllController extends CController

{


   public function actionHandleRequest()

   {


      $route = Yii::app()->getUrlManager()->parseUrl(Yii::app()->getRequest());

      

      try

      {

         Yii::app()->runController($route);

      }

      catch (CHttpException $e)

      {

      

         // Controller is missing...


      } 


   }


}



Oh, I’d read about that a week ago but it didn’t come to my mind for this.

Thanks a bunch!