XF 2.1 Is there anything like internal redirect?

sajal

Active member
Does XF providing anything like internal redirect, where we can catch a certain action of the controller and render a different page than the requested page?

For example, the "forums/" page by default displays a list of forums, but I want to redirect it to some other internal static page created by XF? So that, in the browser it shows me "forums" but displays different output.

I have tried following in the actionIndex method of the "src/XF/Pub/Controller/Forum.php" class:

return $this->rerouteController('XF\Pub\Controller\Page', 'index', ['node_name'=>'my-custom-page']);

It works well and takes me to "pages/my-custom-page", but in browser it also changes the URL in the address bar. I don't want that.
 
The rerouteController() method is what you want, but it will redirect in the case you provided because the action you're rerouting to calls assertCanonicalUrl().
 
Ah, thanks @Jeremy P for your reply. I think I will override the "Index" method of the "src/XF/Pub/Controller/Page.php" class too, and will skip calling to "assertCanonicalUrl" if it's home page.
 
Ah, thanks @Jeremy P for your reply. I think I will override the "Index" method of the "src/XF/Pub/Controller/Page.php" class too, and will skip calling to "assertCanonicalUrl" if it's home page.
A "cleaner" approach might be extending \XF\Pub\Controller\Page and just stubbing out assertCanonicalUrl() itself, perhaps even conditionally depending on the current route.
 
@Jeremy P, Yeah I've created a class extensions for the \XF\Pub\Controller\Page and \XF\Pub\Controller\Forum classes:

The following will pass the route information from where the home-page content will come. I also pass skip_canonical_check from here, so I can check it for only pages come from here.

PHP:
<?php
namespace Demo\Frontpage\XF\Pub\Controller;
use XF\Mvc\ParameterBag;

class Forum extends XFCP_Forum
{
    public function actionIndex(ParameterBag $params)
    {
        ...
        ...

        switch ($this->options()->forumsDefaultPage)
        {
            case 'new_posts':
                return $this->rerouteController(__CLASS__, 'newposts');
                
            case 'forums':
            default:
                // Demo - Change the forum listing page to custom-page
                return $this->rerouteController('XF\Pub\Controller\Page', 'index', ['node_name' => 'my-custom-page', 'skip_canonical_check' => TRUE]);
        }
    }
}

And

PHP:
<?php
namespace Demo\Frontpage\XF\Pub\Controller;

use XF\Mvc\ParameterBag;

class Page extends XFCP_Page
{
    public function actionIndex(ParameterBag $params)
    {
        ...

        // Demo - skip canonical check to prevent redirect
        $arrParams = $params->params();
        if (!isset($arrParams['skip_canonical_check'])) {
            $this->assertCanonicalUrl($this->buildLink('pages', $page));
        }

        ...
    }
}

I hope it's OK and as per standards.

Thanks.
 
@Jeremy P, Yeah I've created a class extensions for the \XF\Pub\Controller\Page and \XF\Pub\Controller\Forum classes:
It might be cleaner to create a class property like $skipUrlCanonicalization that defaults to false and override assertCanonicalUrl to check the property and bail out if it's true. Then in your actionIndex() you can conditionally set the property, call the parent, and then reset the property (probably wise to do so in a try { ... } finally { ... } block).
 
Top Bottom