Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add CWebApplication::onBeforeControllerInit event #1340

Open
twisted1919 opened this Issue · 3 comments

3 participants

twisted1919 Mark Carsten Brandt
twisted1919
/**
 * Creates the controller and performs the specified action.
 * @param string $route the route of the current request. See {@link createController} for more details.
 * @throws CHttpException if the controller could not be created.
 */
/**
 * Creates the controller and performs the specified action.
 * @param string $route the route of the current request. See {@link createController} for more details.
 * @throws CHttpException if the controller could not be created.
 */
public function runController($route)
{
    if(($ca=$this->createController($route))!==null)
    {
        list($controller,$actionID)=$ca;
        $oldController=$this->_controller;
        $this->_controller=$controller;
        // enhancement start
        if($this->hasEventHandler('onBeforeControllerInit'))
            $this->onBeforeControllerInit(new CEvent($controller));
        // enhancement end
        $controller->init();
        $controller->run($actionID);
        $this->_controller=$oldController;
    }
    else
        throw new CHttpException(404,Yii::t('yii','Unable to resolve the request "{route}".',
            array('{route}'=>$route===''?$this->defaultController:$route)));
}

/**
 * Raised right BEFORE CController::init() is called by CWebapplication::runController.
 * @param CEvent $event the event parameter
 */
public function onBeforeControllerInit($event)
{
    $this->raiseEvent('onBeforeControllerInit',$event);
}

Then from any other component, we can just hook inside the controller even if the controller hasn't been instantiated yet (i.e: from preloaded components).

Mark

Hm, looks good for me, but not for controller, maybe in module usage it will be better to do this, provide some new events onBeforeModuleInit and onAfterModuleInit.

twisted1919

I am not sure now, but if defined an onBeforeModuleInit for example, i think you wouldn't have access to the controller instance because at that point the controller is not instantiated.

Adding the events you provided(at least onAfterModuleInit() where you'd probably be able to preload/set aliases/override module vars) beside the one i recommend would make sense if one would like to alter the cwebmodule instance from within another component.

However, i really need a way to hook into the controller instance from any application component, at the point the controller hasn't been instantiated yet.

Carsten Brandt
Collaborator

not sure if we will add it to the core atm. But for now you can raise the event in your base Controller class to add the event for all controllers in you application.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.