Yii Framework Forum: "Admin module" taking over - Yii Framework Forum

Jump to content

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

"Admin module" taking over Rate Topic: -----

#1 User is offline   OriginalCopy 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 63
  • Joined: 13-March 09

Posted 28 February 2010 - 01:54 PM

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).
0

#2 User is offline   Y!! 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 978
  • Joined: 18-June 09

Posted 28 February 2010 - 02:03 PM

View PostOriginalCopy, on 28 February 2010 - 01:54 PM, said:

To recap, I'm looking for a kind of CWebModule::beforeAction(), just like CController::beforeAction() exists.


CWebModule::beforeControllerAction()
0

#3 User is offline   OriginalCopy 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 63
  • Joined: 13-March 09

Posted 28 February 2010 - 02:21 PM

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

#4 User is offline   Y!! 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 978
  • Joined: 18-June 09

Posted 28 February 2010 - 02:55 PM

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

      } 

   }

}

0

#5 User is offline   OriginalCopy 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 63
  • Joined: 13-March 09

Posted 28 February 2010 - 03:00 PM

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

Thanks a bunch!
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