How to redirect another page instead of redirecting to abort 403 error page in Laravel 5.4 - laravel

My question is whenever there is a 403 error I should redirect to my own custom page or show the flash message(on the same page) something like that.How can I achieve this in Laravel?.

You can use app/Exceptions/Handler.php for this
public function render($request, Exception $e)
{
if ($e->getStatusCode() == 403) {
return redirect('yourpage'); // this will be on a 403 exception
}
return parent::render($request, $e); // all the other exceptions
}

You can create a view for specific HTTP error codes. If you set up a Blade template at resources/views/errors/403.blade.php, it will get used for all 403 error responses.
Source
Alternatively you can set up a custom exception handler for 403 responses if you need something more involved. I found a good example of this here.

you can make you own page in inside the view .resources/views/errors/yourblade.blade.php
after that just return your page to that page.

You can use app/Exceptions/Handler.php for this as Thomas Moors stated above or you can use a try catch for basic error handling.
try {
do Something //
}
catch (Exception $e) {
//log error in log file or dv
return redirect->('home');
}

Related

Laravel redirect ALL types of errors to custom view

I'm looking for a way to catch all possible errors and redirect all types of errors to 1 page.
My current code in /Exceptions/Handler.php:
if ($this->isHttpException($exception)) {
$statusCode = $exception->getStatusCode();
switch ($statusCode) {
case '404':
return response()->view('layouts/404');
}
}
Problem is that ErrorException (E_NOTICE) types (which are caused by possible bugs in the code) aren't redirected to the 404 page. These errors end up on the 'Woops something went wrong' page.
I basically am trying to make every type of error end up on my custom error page.
All attempts end up on white pages.
What am I not seeing?
Well, to do what you're asking for you should go to App\Exceptions\Handler.php and there, you need to modify the render method:
public function render($request, Exception $exception)
{
abort(404);
}
But I insist, this is a terrible idea, you should catch the Exceptions that your app may get and show the error message to the user in a clean way, for example:
try{
//... your fancy code here
}catch(Exception $e){
// return with the message error $e->getMessage()
}

Design 500 error page only for production

I'm looking for a modern (Laravel 5.4) way to display custom 500 error page only for HTTP (non ajax/fetch) response. I read some threads but each response looks like a trick or is outdated. There is probably something to modify in \App\Exceptions\Handler, but I did not find the "right way".
Is there a simple way to display a specific page on fatal error (uncatched, returning 500) in Laravel 5.4?
In other words, when I have a syntax error on one of my controller, it displays "Whoops something went wrong" with some HTML and 500 error code. I would like to display something else, with the same rules as default behavior (ideally only for HTML browser, not for ajax/fetch, etc.).
EDIT: only in production environment.
Laravel makes it easy to display custom error pages for various HTTP status codes. For example, if you wish to customize the error page for 404 HTTP status codes, create a resources/views/errors/404.blade.php. This file will be served on all 404 errors generated by your application. The views within this directory should be named to match the HTTP status code they correspond to. The HttpException instance raised by the abort function will be passed to the view as an $exception variable.
https://laravel.com/docs/5.4/errors#custom-http-error-pages
From the selected "best answer" of this thread: https://laracasts.com/discuss/channels/general-discussion/custom-error-page-er500
You could modify \App\Exceptions\Handler::render():
public function render($request, Exception $exception)
{
if (config('app.debug') && !$this->isHttpException($exception)) {
$exception = new \Symfony\Component\HttpKernel\Exception\HttpException(500);
}
return parent::render($request, $exception);
}
Your exception will be reported in the logs as usually, but woops page will be replaced by your 500.blade.php view.
Sometimes you have to catch the specific exception in order to render the error view. in Laravel 5.4 you can do this by editing the report() method in the App\Exceptions\Handler class
public function report(Exception $exception)
{
if ($exception instanceof CustomException) {
// here you can log the error and return the view, redirect, etc...
}
return parent::report($exception);
}

Laravel return Csrf error as json

I'm building a mobile app and when a CSRF token isn't present I want to return an error in JSON instead of it returning the "TokenMismatchException" Html page.
Is there anyway to do this easily without adjusting the library code?
You can create your own custom responses and make one for TokenMismatchException
So you do that in the Exceptions/Handler.php file. Something like;
public function render($request, Exception $e)
{
if($e instanceof TokenMismatchException)
{
return json(......
}
I think you might also need to include in the use statement;
use Illuminate\Session\TokenMismatchException as TokenMismatchException;

Creating custom error page in Lumen

How do I create custom view for errors on Lumen? I tried to create resources/views/errors/404.blade.php, like what we can do in Laravel 5, but it doesn't work.
Errors are handled within App\Exceptions\Handler. To display a 404 page change the render() method to this:
public function render($request, Exception $e)
{
if($e instanceof NotFoundHttpException){
return response(view('errors.404'), 404);
}
return parent::render($request, $e);
}
And add this in the top of the Handler.php file:
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
Edit: As #YiJiang points out, the response should not only return the 404 view but also contain the correct status code. Therefore view() should be wrapped in a response() call passing in 404 as status code. Like in the edited code above.
The answer by lukasgeiter is almost correct, but the response made with the view function will always carry the 200 HTTP status code, which is problematic for crawlers or any user agent that relies on it.
The Lumen documentation tries to address this, but the code given does not work because it is copied from Laravel's, and Lumen's stripped down version of the ResponseFactory class is missing the view method.
This is the code which I'm currently using.
use Symfony\Component\HttpKernel\Exception\HttpException;
[...]
public function render($request, Exception $e)
{
if ($e instanceof HttpException) {
$status = $e->getStatusCode();
if (view()->exists("errors.$status")) {
return response(view("errors.$status"), $status);
}
}
if (env('APP_DEBUG')) {
return parent::render($request, $e);
} else {
return response(view("errors.500"), 500);
}
}
This assumes you have your errors stored in the errors directory under your views.
It didn't work for me, but I got it working with:
if($e instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException) {
return view('errors.404');
}
You might also want to add
http_response_code(404)
to tell the search engines about the status of the page.

Laravel default route to 404 page

I'm using Laravel 4 framework and I've defined a whole bunch of routes, now I wonder for all the undefined urls, how to route them to 404 page?
In Laravel 5.2. Do nothing just create a file name 404.blade.php in the errors folder , it will detect 404 exception automatically.
Undefined routes fires the Symfony\Component\HttpKernel\Exception\NotFoundHttpException exception which you can handle in the app/start/global.php using the App::error() method like this:
/**
* 404 Errors
*/
App::error(function(\Symfony\Component\HttpKernel\Exception\NotFoundHttpException $exception, $code)
{
// handle the exception and show view or redirect to a diff route
return View::make('errors.404');
});
The recommended method for handling errors can be found in the Laravel docs:
http://laravel.com/docs/4.2/errors#handling-404-errors
Use the App::missing() function in the start/global.php file in the following manner:
App::missing(function($exception)
{
return Response::view('errors.missing', array(), 404);
});
according to the official documentation
you can just add a file in: resources/views/errors/
called 404.blade.php with the information you want to display on a 404 error.
I've upgraded my laravel 4 codebase to Laravel 5, for anyone who cares:
App::missing(function($exception) {...});
is NO LONGER AVAILABLE in Laravel 5, in order to return the 404 view for all non-existent routes, try put the following in app/Http/Kernel.php:
public function handle($request) {
try {
return parent::handle($request);
}
catch (Exception $e) {
echo \View::make('frontend_pages.page_404');
exit;
// throw $e;
}
}

Resources