1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

controllerName & viewName in templates listener

Discussion in 'XenForo Development Discussions' started by cclaerhout, Feb 6, 2013.

  1. cclaerhout

    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.
  2. Chris D

    Chris D XenForo Developer Staff Member

    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.
    cclaerhout likes this.
  3. cclaerhout

    cclaerhout Well-Known Member

    I might also use TMS to add a parameter to the hook.

    Thank you !
    Chris D likes this.
  4. MOZ

    MOZ Well-Known Member

    I am liking where this is going ;)
  5. cclaerhout

    cclaerhout Well-Known Member

    The solution was simpler:
    $controllerName $template->getParam('controllerName');
    $controllerAction $template->getParam('controllerAction');
    $viewName $template->getParam('viewName');
  6. Robbo

    Robbo Well-Known Member

    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...
    cclaerhout likes this.
  7. cclaerhout

    cclaerhout Well-Known Member

    I didn't see your reply. The method to use a single class with different listeners is clever. Thanks !
  8. cclaerhout

    cclaerhout Well-Known Member

    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:
        protected static $_controllerInfo = array('controllerName' => NULL'controllerAction' => NULL'viewName' => NULL);

        public static function 
    getController(XenForo_FrontController $fcXenForo_ControllerResponse_Abstract &$controllerResponseXenForo_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;

    //Rest of the code of the class where the self::$_controllerInfo can be used anywhere
    Boothby likes this.
  9. Robbo

    Robbo Well-Known Member

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

    cclaerhout Well-Known Member

    Another way to do it:
    $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;

Share This Page