eric.itzhak
eric.itzhak

Reputation: 16062

get current request in 404 page

Hey I want to use the current request object as the facade not the static way($request not Request::) in a custom 404 blade file.

I don't know if I can hint about it to the error handler or is there a way to create that object?

Should/Could I do it via the Expections/Handler.php file?

I've found Here the following answer:

//Create a view and set this code in app/Exception/Handler.php :

/**
 * Render an exception into a response.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Exception  $e
 * @return \Illuminate\Http\Response
 */
public function render($request, Exception $e)
{
    if($e instanceof NotFoundHttpException)
    {
        return response()->view('missing', [], 404);
    }
    return parent::render($request, $e);
}

 //Set this use to get it working :

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

Is this the right way to do it?

Upvotes: 0

Views: 202

Answers (1)

thefallen
thefallen

Reputation: 9749

Yes, you can do it from the Handler. Inside the render() method:

if ($e instanceof NotFoundHttpException) {
    return response()->view('your.view.name', $dataYouWantToPass);
}

Upvotes: 1

Related Questions