Skip to content

Instantly share code, notes, and snippets.

@parotikov
Created August 1, 2017 08:38
Show Gist options
  • Save parotikov/5e6aaa0a893acd8e26922b2402c4ee93 to your computer and use it in GitHub Desktop.
Save parotikov/5e6aaa0a893acd8e26922b2402c4ee93 to your computer and use it in GitHub Desktop.
/**
* Redirect the user no matter what. No need to use a return
* statement. Also avoids the trap put in place by the Blade Compiler.
*
* @param string $url
* @param int $code http code for the redirect (should be 302 or 301)
*/
function redirect_now($url, $code = 302)
{
try {
\App::abort($code, '', ['Location' => $url]);
} catch (\Exception $exception) {
// the blade compiler catches exceptions and rethrows them
// as ErrorExceptions :(
//
// also the __toString() magic method cannot throw exceptions
// in that case also we need to manually call the exception
// handler
$previousErrorHandler = set_exception_handler(function () {
});
restore_error_handler();
call_user_func($previousErrorHandler, $exception);
die;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment