controllerName & viewName in templates listener

cclaerhout

Well-known member
When we want to make a simple conditional based on the controller name or the view name it's easy inside the templates (see here). But to do the same inside the templates listeners is not as easy (hook & postrender). I've found the solution of DigitalPoint (here) & the one of Rigel (here).

What is the best solution? Isn't there a simpler way?

Purpose: having two different kinds of layout for the editor (full - simple/mobile). Simple layout would be activated when a certain controller/view type is matched or if the user is on a mobile device. Otherwise, it will be the full layout that will be displayed. This function could be integrated in the buttons manager.
 
Well, there's nothing more simple than digitalpoint's method.

Rigel's may be a little bit more robust, though.

It might be a toss a coin moment.
 
When we want to make a simple conditional based on the controller name or the view name it's easy inside the templates (see here). But to do the same inside the templates listeners is not as easy (hook & postrender). I've found the solution of DigitalPoint (here) & the one of Rigel (here).

What is the best solution? Isn't there a simpler way?

Purpose: having two different kinds of layout for the editor (full - simple/mobile). Simple layout would be activated when a certain controller/view type is matched or if the user is on a mobile device. Otherwise, it will be the full layout that will be displayed. This function could be integrated in the buttons manager.
I am liking where this is going ;)
 
The solution was simpler:
PHP:
$controllerName = $template->getParam('controllerName');
$controllerAction = $template->getParam('controllerAction');
$viewName = $template->getParam('viewName');
 
All those ways are just eww...

Use a single Listener class. Have a protected static $frontController variable. Create a listener that has access to the front controller. Do self::$frontController = $frontController;. Use it in other listeners with self::$frontController.

Although the last way you said is okay. The ways in your first post are hacky and I expect better from those guys...
 
All those ways are just eww...

Use a single Listener class. Have a protected static $frontController variable. Create a listener that has access to the front controller. Do self::$frontController = $frontController;. Use it in other listeners with self::$frontController.
I didn't see your reply. The method to use a single class with different listeners is clever. Thanks !
 
Just a feedback, here is an example of the Robbo solution (I'm not sure it's 100% the same since there were frontController listeners):

Use this listener front_controller_pre_view and a code like this:
PHP:
    protected static $_controllerInfo = array('controllerName' => NULL, 'controllerAction' => NULL, 'viewName' => NULL);

    public static function getController(XenForo_FrontController $fc, XenForo_ControllerResponse_Abstract &$controllerResponse, XenForo_ViewRenderer_Abstract &$viewRenderer, array &$containerParams)
    {
        self::$_controllerInfo['controllerName'] = (isset($controllerResponse->controllerName)) ? $controllerResponse->controllerName : NULL;
        self::$_controllerInfo['controllerAction'] = (isset($controllerResponse->controllerAction)) ? $controllerResponse->controllerAction : NULL;
        self::$_controllerInfo['viewName'] = (isset($controllerResponse->viewName)) ? $controllerResponse->viewName : NULL;

        //Zend_Debug::dump(self::$_controllerInfo);
    }
 
    //Rest of the code of the class where the self::$_controllerInfo can be used anywhere
 
That code could look a little better but that is what I meant pretty much. For example there is no need to use an array there, it serves no purpose other than to compact the code. This reminds me of why I hate listeners.
 
Another way to do it:
PHP:
$fc = XenForo_Application::get('fc');
$route = $fc->route();
$controllerName = $route->getControllerName();
$jsonResponse = ($route->getResponseType() == 'json');
/*If more data is needed*/
$data = $fc->dispatch($route);
$controllerName = $data->controllerName;
$controllerAction = $data->controllerAction;
$viewName = $data->viewName;
 
Back
Top Bottom