XF 2.2 Interrupting the default route

Lee

Well-known member
If I wanted to interrupt the default route and replace it with something else, based on a preference in the user settings - would that be possible..?

If so, where should I look to extend?
 
PHP:
        $container['router.public'] = function (Container $c)
        {
            $class = $this->extendClass('XF\Mvc\Router');

            /** @var \XF\Mvc\Router $r */
            $r = new $class($c['router.public.formatter'], $c['router.public.routes']);
            // ...
            $r->setIndexRoute($c['options']->indexRoute ?: 'forums/');
            //...

            $this->fire('router_public_setup', [$c, &$r]);

            return $r;
        };
So you will need to make use of router_public_setup code event listener and then make a call which would look something like this:
PHP:
    public static function routerPublicStep(Container $c, Router &$router) : void
    {
        $router->setIndexRoute(\XF::visitor()->Option->abc_xyz_mg_as_index ? 'xfmg/' : 'xfrm');
    }
 
PHP:
        $container['router.public'] = function (Container $c)
        {
            $class = $this->extendClass('XF\Mvc\Router');

            /** @var \XF\Mvc\Router $r */
            $r = new $class($c['router.public.formatter'], $c['router.public.routes']);
            // ...
            $r->setIndexRoute($c['options']->indexRoute ?: 'forums/');
            //...

            $this->fire('router_public_setup', [$c, &$r]);

            return $r;
        };
So you will need to make use of router_public_setup code event listener and then make a call which would look something like this:
PHP:
    public static function routerPublicStep(Container $c, Router &$router) : void
    {
        $router->setIndexRoute(\XF::visitor()->Option->abc_xyz_mg_as_index ? 'xfmg/' : 'xfrm');
    }
Thanks @TickTackk, amazingly helpful. :)
 
I now have this:

PHP:
<?php

namespace TLS\DefaultRoute;

use XF\Mvc\Router;

class RouteExtension {

public static function routerPublicStep($c, &$router) : void
    {
       $router->setIndexRoute(\XF::visitor()->Profile->custom_fields->HomePage ? 'whats-new/' : '0');
       $router->setIndexRoute(\XF::visitor()->Profile->custom_fields->HomePage ? 'members/' : '1');

    }

}

With a drop down "HomePage" as a custom user field. This then has two options values "0" and "1".

I am getting the following error when trying to view the default route:

Oops! We ran into some problems.​


The requested page could not be found. (Code: no_controller, controller: -, action: -)
 
Try dumping \XF::visitor()->Profile->custom_fields->HomePage and see what value it shows? Also, why do you have two setIndexRoute() calls?
 
Yes, that dump displays the number as required.

I am trying to make a drop down that changes the default route depending on what option is selected.

So 0 = index
1 = forum-list
2 = whats-new

etc...
 
You want to do something like this:
PHP:
$newDefaultRoute = null;

if (\XF::visitor->Option->blah === 0)
{
    // default
}
else if (\XF::visitor->Option->blah === 1)
{
    $newDefaultRoute = 'find-new';
}
else if (\XF::visitor->Option->blah === 3)
{
    $newDefaultRoute = 'xfrm'
}
if (is_string($newDefaultRoute))
{
   $router->setIndexRoute($newDefaultRoute);
}
Basically, getting used to basic PHP syntax first. :P
 
You want to do something like this:
PHP:
$newDefaultRoute = null;

if (\XF::visitor->Option->blah === 0)
{
    // default
}
else if (\XF::visitor->Option->blah === 1)
{
    $newDefaultRoute = 'find-new';
}
else if (\XF::visitor->Option->blah === 3)
{
    $newDefaultRoute = 'xfrm'
}
if (is_string($newDefaultRoute))
{
   $router->setIndexRoute($newDefaultRoute);
}
Basically, getting used to basic PHP syntax first. :p

Thanks @TickTackk - using the above code I have managed to get what I am looking for :)
 
My next question, I have used a custom user field here to achieve this.

Is there a better way to create the drop down in "user preferences" that I could use to then package up within the addon?
 
You would have to do couple of things for that:
  1. Add a new column under xf_user_option table
  2. Extend the \XF\Entity\UserOption entity's getStructure() to add the newly added column
  3. Update Setup.php to add the new column on install and drop on uninstall
  4. Add template modification to add <xf:selectrow /> in user settings page
  5. Extend \XF\Pub\Controller\Account controller and then extend accountSaveProcess() or whatever that method was called to store the value from newly added <xf:selectrow />
 
  • Love
Reactions: Lee
Back
Top Bottom