How do I catch exceptions missing pages in Laravel 5

0 votes
In Laravel 5, App::missing and App::error is not available, so how do your catch exceptions and missing pages now?
Aug 11, 2020 in Laravel by kartik
• 37,520 points
2,016 views

1 answer to this question.

0 votes

Hello @kartik,

In Laravel 5 you can catch exceptions by editing the render method in app/Exceptions/Handler.php.

If you want to catch a missing page (also known as NotFoundException) you would want to check if the exception, $e, is an instance of Symfony\Component\HttpKernel\Exception\NotFoundHttpException.

public function render($request, Exception $e) {
    if ($e instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException)
        return response(view('error.404'), 404);

    return parent::render($request, $e);
}

With the code above, we check if $e is an instanceof of Symfony\Component\HttpKernel\Exception\NotFoundHttpException and if it is we send a response with the view file error.404 as content with the HTTP status code 404.

This can be used to ANY exception. So if your app is sending out an exception of App\Exceptions\MyOwnException, you check for that instance instead.

public function render($request, Exception $e) {
    if ($e instanceof \App\Exceptions\MyOwnException)
        return ''; // Do something if this exception is thrown here.

    if ($e instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException)
        return response(view('error.404'), 404);

    return parent::render($request, $e);
}

Hope this helps!!

Thank You!!

answered Aug 11, 2020 by Niroj
• 82,840 points

Related Questions In Laravel

0 votes
1 answer

How do I include partials from a blade layout in laravel?

Hello @kartik, You need to give the full ...READ MORE

answered Nov 2, 2020 in Laravel by Niroj
• 82,840 points
4,168 views
0 votes
1 answer

How do I set up phpMyAdmin on a Laravel Homestead box?

Hello, Step 1: Go to the phpMyAdmin website, download the latest ...READ MORE

answered Mar 31, 2020 in Laravel by Niroj
• 82,840 points
7,319 views
+1 vote
1 answer

How can I run specific migration in laravel?

Hello @kartik, Use this command it worked for ...READ MORE

answered Aug 4, 2020 in Laravel by Niroj
• 82,840 points
23,073 views
0 votes
1 answer

How Can I Remove “public/index.php” in the URL Generated Laravel?

Hello @kartik, If it isn't already there, create ...READ MORE

answered Aug 10, 2020 in Laravel by Niroj
• 82,840 points
13,466 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
22,974 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
2,974 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
2,903 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
44,435 views
0 votes
1 answer

How do I get HTTP Request body content in Laravel?

Hello @kartik, Inside controller inject Request object. So ...READ MORE

answered Aug 11, 2020 in Laravel by Niroj
• 82,840 points
17,012 views
0 votes
1 answer

How do I get raw form data in laravel?

Hii @kartik, Laravel intercepts all input. If you're ...READ MORE

answered Oct 28, 2020 in Laravel by Niroj
• 82,840 points
4,688 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP