XF 1.5 Call controller action from other controller

grisha2217

Active member
Hi guys.
I have extended controller:
PHP:
class Test_ForumSearchThreads_ControllerPublic_Thread extends XFCP_Test_ForumSearchThreads_ControllerPublic_Thread
{
    /* XenForo_ControllerPublic_Forum $forumController */
    private $forumController;

    public function __construct(Zend_Controller_Request_Http $request, Zend_Controller_Response_Http $response, XenForo_RouteMatch $routeMatch) {
        parent::__construct($request, $response, $routeMatch);
        $this->forumController = new XenForo_ControllerPublic_Forum($request, $response, $routeMatch);
    }

    public function actionIndex()
    {
        $parent = parent::actionIndex();
        if ($parent instanceof XenForo_ControllerResponse_View)
        {
            if (// not important condition)
            {
                $fstModel = $this->_getForumSearchThreadsModel();
                 // here i set some params in forum controller request (node_id, page etc)
                 $response = $this->forumController->actionForum();
            }
        }

        return $parent;
    }
}


When i try get $response value, xenforo redirect me to some forum, how i can get params from forum controller response? Thanks.
 
If you're trying to reroute to another controller action:
PHP:
return $this->responseReroute('XenForo_ControllerPublic_Forum', 'forum');

Thread lists are canonicalized though, so a redirect is expected here. If you want to get or set some parameters inside the forum controller, you will have to extend it instead of the thread controller.
 
If you're trying to reroute to another controller action:
PHP:
return $this->responseReroute('XenForo_ControllerPublic_Forum', 'forum');

Thread lists are canonicalized though, so a redirect is expected here. If you want to get or set some parameters inside the forum controller, you will have to extend it instead of the thread controller.

I want to parse get threads in certain forum in Thread Controller, i don't want copy past code from actionForum to thread controller.

Also, i tried to extend Forum controller, i coded callActionForum function - is a copy of actionForum function, but return value is $viewParams (not responseView). It is not working (xenforo redirect me again)

PHP:
<?php
class Test_ForumSearchThreads_ControllerPublic_Thread extends XFCP_Test_ForumSearchThreads_ControllerPublic_Thread
{
    /* Test_ForumSearchThreads_ControllerPublic_Forum $forumController */
    private $forumController;

    public function __construct(Zend_Controller_Request_Http $request, Zend_Controller_Response_Http $response, XenForo_RouteMatch $routeMatch)
    {
        parent::__construct($request, $response, $routeMatch);
        $controller = XenForo_Application::resolveDynamicClass('XenForo_ControllerPublic_Forum', 'controller');
        $this->forumController = new $controller($request, $response, $routeMatch);
    }

    public function actionIndex()
    {
        $parent = parent::actionIndex();
        if ($parent instanceof XenForo_ControllerResponse_View)
        {
            if (XenForo_Visitor::getUserId() === 1)
            {
                $fstModel = $this->_getForumSearchThreadsModel();
                $requestParams = $fstModel->getRequestParamsFromCookie($parent->params['thread']['node_id']);
                if ($requestParams)
                {
                    foreach ($requestParams AS $key => $value)
                    {
                        $this->_request->setParam($key, $value);
                    }

                    var_dump($this->forumController->callActionForum());
                }


            }
        }

        return $parent;
    }
}
 
If you're trying to reroute to another controller action:
PHP:
return $this->responseReroute('XenForo_ControllerPublic_Forum', 'forum');

Thread lists are canonicalized though, so a redirect is expected here. If you want to get or set some parameters inside the forum controller, you will have to extend it instead of the thread controller.
oh man, i understood what you mean. I just overrided canonicalizeRequestUrl function:
Code:
public function canonicalizeRequestUrl($linkUrl)
{
    // empty overrided function
}


THANKS!!!!
 
I want to parse get threads in certain forum in Thread Controller, i don't want copy past code from actionForum to thread controller.
Generally speaking, controller actions aren't meant to be called in this way (directly). It's probably a more sane approach to just copy/paste the code if you need it. If you're just looking to retrieve a set of threads in a forum, calling the action probably comes with additional overhead anyways (depending on what you're doing with it). If you are trying to display a thread list on your page, fetching the list with ajax may also be a viable option.

oh man, i understood what you mean. I just overrided canonicalizeRequestUrl function:
Just keep in mind that this means routes to this controller will not be properly canonicalized. If you take this approach, consider coming up with a special conditional for your page and returning the parent method if it's not a match.
 
Top Bottom