user11570960
user11570960

Reputation:

Laravel NotFoundHttpException Handling

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.

Upvotes: 3

Views: 4370

Answers (7)

Sumon Sarker
Sumon Sarker

Reputation: 129

In your

app>Exception>Handler.php

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

Just put the below code under the register method

   $this->renderable(function (NotFoundHttpException $e, $request) {
        if ($request->is('api/*')) { 
           // Here *api* is route prefix just validate your route 
           // also you can use withot checking the request but it's not good

           // If you want you can return view or json response from here 
            return response()->view('errors.invalid-order', [], 500);
           // Or
            return response()->json([
                'message' => 'Record not found.'
            ], 404);
        }
    });

Upvotes: 0

Fan Zhang
Fan Zhang

Reputation: 1

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

Upvotes: 0

Kulshreshth K
Kulshreshth K

Reputation: 1176

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.

Upvotes: 4

EZone Lai
EZone Lai

Reputation: 106

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);
    }

Upvotes: 1

Chester
Chester

Reputation: 722

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

Upvotes: 5

Rajkumar Sharma
Rajkumar Sharma

Reputation: 584

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');
    }
}

Upvotes: 1

PHP Geek
PHP Geek

Reputation: 4033

write in top of controller

use Exception; 

Upvotes: 1

Related Questions