Automatic Redirection

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?
 
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.
 
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.
Thanks Jake,

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

PHP:
	public static function frontControllerPreView(XenForo_FrontController $fc, XenForo_ControllerResponse_Abstract &$controllerResponse, XenForo_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.
 
Top Bottom