Created
October 11, 2016 13:36
-
-
Save molayli/2cde1317bb81fd2734fdd1a6f63ce46c to your computer and use it in GitHub Desktop.
Laravel redirect without return statment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//..... | |
if(!function_exists('freeRedirect')){ | |
function freeRedirect($to = '/'){ | |
throw new \Illuminate\Http\Exception\HttpResponseException(redirect($to)); | |
} | |
} | |
//............. | |
//usage | |
class AbcController extends Controller{ | |
public function aMethod(){ | |
freeRedirect('/to/another/route'); | |
} | |
} |
Thank you!
Very helpful! Not only redirect, you can return any kind of response.
I used this to return view as well.
if(!function_exists('abortToView')){
function abortToView($path){
throw new \Illuminate\Http\Exception\HttpResponseException(response(view($path)));
}
}
For laravel 8 users just change the namespace to Exceptions
if(!function_exists('abortToView')){
function abortToView($path){
throw new \Illuminate\Http\Exceptions\HttpResponseException(response(view($path)));
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this. using this method you can also attach flash data i.e.
throw new HttpResponseException(redirect('/to/another/route/')->with('status', 'An error occurred.'));
Also HttpResponseException moved and now lives at Illuminate\Http\Exceptions\HttpResponseException