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

Automatic Redirection

Discussion in 'XenForo Development Discussions' started by Chris D, Feb 24, 2013.

  1. Chris D

    Chris D XenForo Developer Staff Member

    Here's my plan...

    I'm setting a parameter on the $visitor object using the visitor_setup event.

    In another event I want to check that parameter (so the $visitor object needs to be available) and then perform a controller redirection if the parameter is true.

    I assumed this would be do-able in the controller_pre_dispatch event, but despite using:

    return $controller->responseRedirect ... etc...

    The redirection doesn't occur.

    Of course I could check for that parameter in a template_hook event and if it's true inject a <meta refresh client side redirect into the HTML but... (n) don't like it... and the redirection is pretty much visible to the end user so it just doesn't seem clean.

    I'm sure there's an elegant solution... any ideas?
  2. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    Use front_controller_pre_view. That is the first "front" event where you have access to the controller response. But you don't have access to the controller object at that point so you can't call responseRedirect() from the controller. You can duplicate that function for your purposes.
    Chris D likes this.
  3. Chris D

    Chris D XenForo Developer Staff Member

    Thanks Jake,

    Thanks to your advice, I've come up with this that works perfectly:

        public static function frontControllerPreView(XenForo_FrontController $fcXenForo_ControllerResponse_Abstract &$controllerResponseXenForo_ViewRenderer_Abstract &$viewRenderer, array &$containerParams)
    $dependencies $fc->getDependencies();
    $request $fc->getRequest();
    $requestUri $request->getRequestUri();

            if (!
    strstr($requestUri'/account/')  && $dependencies instanceof XenForo_Dependencies_Public)
    $redirect = new XenForo_ControllerResponse_Redirect();
    $redirect->redirectType XenForo_ControllerResponse_Redirect::SUCCESS;
    $redirect->redirectTarget XenForo_Link::buildPublicLink('account');

    $controllerResponse $redirect;            
    EDIT: I have just updated the above code. It's important (usually) not to impose a forced, automatic redirection from admin pages ;) Luckily the front controller object contains the dependencies which is either an instance of XenForo_Dependencies_Public or XenForo_Dependencies_Admin.
    Sadik B and Bob like this.

Share This Page