2
votes

I am Unable to handle NotFoundHttpException in Laravel

I have already tried to handle it through render() in Handler.php


// Handler.php 
// render()
==============

public function render($request, Exception $e)
    {
        if($e instanceof NotFoundHttpException){
            return response('Not Found');
        }
        return parent::render($request,$e);
   }

It should return the response 'Not Found'. But it is returning

Class 'Symfony\Component\HttpKernel\Exception\NotFoundHttpException' not found

Which means that NotFoundHttpException class cannot be found in vendor folder, I tried to look for the class in the path specified but I could not find it.

6
When adding use Exception; what error you are getting? - Shivendra Singh

6 Answers

3
votes
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
3
votes

You can try it like this:

<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

class Handler extends ExceptionHandler
{

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $exception
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $exception)
    {
        if ($this->isHttpException($exception)) {
            $statusCode = $exception->getStatusCode();
            switch ($statusCode) {
                case '404':
                    return response()->view('errors/404');
                case '500';
                    return response()->view('errors/500');
            }
        }
        return parent::render($request, $exception);
    }

}

Merge this code in your handler.php instead of pasting it directly.

1
votes

write in top of controller

use Exception; 
1
votes

In handler.php

public function render($request, Exception $exception)
    {
        if ($this->isHttpException($exception)) {
            switch ($exception->getStatusCode()) {
                case 404:
                    return redirect()->route('error404');
                    break;
                case '500':
                    return redirect()->route('error500');
                    break;
                default:
                    return redirect()->route('errordefault');
                    break;
            }
        }
        else{
            return parent::render($request,$exception);
        }
        return parent::render($request, $exception);
    }

And then in web.php you can route to your desire Controller, here I have handled with self made ErrorController.php

//Error page
Route::get('errors', 'ErrorController@errordefault')->name('errordefault');
Route::get('404', 'ErrorController@error404')->name('error404');
Route::get('500', 'ErrorController@error500')->name('error500');

Make function errordefault(), error404() and error500() in ErrorController;

In ErrorController.php

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;

class ErrorController extends Controller
{

    public function error404()
    {
        return view('error_page');
    }
    public function error500()
    {
        return view('error500_page');
    }
    public function errordefault()
    {
        return view('default_error_page');
    }
}

1
votes

you can try this in laravel

     try{
        $response= model::where('uid',$uid)->update($insertArry);
        if($response){
            $backMessage = "insert success";
            $status='success';
            return redirect()->route('website')->with($status, $backMessage);

        }else{
            $backMessage = "something wrong!";
            $status='danger';
            return redirect()->route('website')->with($status, $backMessage);
        }
    }catch (Exception $e) {
        //get exception here
        $backMessage = $e;
        $status='info';
        return redirect()->route('website')->with($status, $backMessage);
    }
0
votes

Try dd($excpetion); Sometimes is another exception like Illuminate\Database\Eloquent\ModelNotFoundException.