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

How to get the current route in actionIndex of ControllerPublic?

Discussion in 'XenForo Development Discussions' started by Scharesoft, Jul 21, 2015.

  1. Scharesoft

    Scharesoft Active Member

    I'm creating an addon for our forum and there I need the current route.

    I created a new area with the url "tes". There will be sub areas like tes/skyrim or tes/oblivion. Now I need this route (skyrim or oblivion) in my actionIndex of the ControllerPublic php file.

    Route/Prefix file:
    PHP:
    class TESPortalGameInformation_Route_Prefix_Game implements XenForo_Route_Interface
    {
        public function 
    match($routePathZend_Controller_Request_Http $requestXenForo_Router $router)
        {
            
    $controller 'TESPortalGameInformation_ControllerPublic_Game';

            return 
    $router->getRouteMatch($controller'''tes');
        }
    }
    ControllerPublic file
    PHP:
    class TESPortalGameInformation_ControllerPublic_Game extends XenForo_ControllerPublic_Abstract
    {
        public function 
    actionIndex()
        {    
             
    $params = array( ... );
             return 
    $this->responseView('TESPortalGameInformation_ViewPublic_Game''tesportal_game_info'$params);
        }
    }
    It would be possible to create a new actionXYZ for each route, but I think it's better to handle it in the actionIndex.

    How to get the current route? Many thanks in advance!
     
    Last edited: Jul 21, 2015
  2. Lawrence

    Lawrence Well-Known Member

    Off the top of my head...

    PHP:
    class TESPortalGameInformation_Route_Prefix_Game implements XenForo_Route_Interface
    {
        public function 
    match($routePathZend_Controller_Request_Http $requestXenForo_Router $router)
        {
            
    $controller 'TESPortalGameInformation_ControllerPublic_Game';

            
    $path explode('/'$router->getRoutePath($request));
            
    $request->setParam('subArea'$path[0]);

            return 
    $router->getRouteMatch($controller$router->resolveActionWithStringParam($routePath$request'action'), 'tes');
        }
    }
    Depending on the route, the sub area you are looking for may be in $path[1], you will have to check for this.

    PHP:
    class TESPortalGameInformation_ControllerPublic_Game extends XenForo_ControllerPublic_Abstract
    {
        public function 
    actionIndex()
        {  
             
    $subArea $this->_input->filterSingle('subArea'XenForo_Input::STRING);

             
    $params = array( ... );
             return 
    $this->responseView('TESPortalGameInformation_ViewPublic_Game''tesportal_game_info'$params);
        }
    }
     
    Lil.Tee, Scharesoft and katsulynx like this.
  3. Scharesoft

    Scharesoft Active Member

    Great, thanks for the help :) It's working after checking the size of the path array and get the last element:

    PHP:
    class TESPortalGameInformation_Route_Prefix_Game implements XenForo_Route_Interface
    {
        public function 
    match($routePathZend_Controller_Request_Http $requestXenForo_Router $router)
        {
            
    $controller 'TESPortalGameInformation_ControllerPublic_Game';

            
    $path explode('/'$router->getRoutePath($request));
            if(isset(
    $path[1])) {
                    
    $request->setParam('subArea'$path[1]);
            }

            return 
    $router->getRouteMatch($controller'''tes');
        }
    }
    PHP:
    class TESPortalGameInformation_ControllerPublic_Game extends XenForo_ControllerPublic_Abstract
    {
        public function 
    actionIndex()
        {
            
    $game $this->_input->filterSingle('subArea'XenForo_Input::STRING);

            
    $params = array( ... );

            return 
    $this->responseView('TESPortalGameInformation_ViewPublic_Game''tesportal_game_info'$params);
        }
    }
     
    Last edited: Jul 21, 2015
    Lil.Tee and Lawrence like this.

Share This Page