Using Variables in Ravel 5.1 404.blade.php

Asked 2 years ago, Updated 2 years ago, 54 views

I am trying to display /views/errors/404.blade.php when you access a URL that does not have a route configured in routes.php, but
Is it possible to use the controller?

I would like to display the variables (for example, displaying the login username) given by the controller in the 404.blade.php.

php laravel

2022-09-30 18:15

1 Answers


if you just want to see the login username in 404.blade.php {{ Auth::user()->name}}
Then it's OK.

However, if you want to pass other variables to the template,
Edit app/Exceptions/Handler.php.

use Symphony\Component\HttpKernel\Exception\HttpException; // Add around line 7

protected function renderHttpException (HttpException$e) // Add to Handler Class
{
    $status=$e->getStatusCode();
    $var="Aiueo";
    if(view()->exists("errors.{$status}"){
        return response()->view("errors.{$status}", ['exception'=>$e, 'var'=>$var], $status);
    } else{
        return$this->convertExceptionToResponse($e);
    }
}

vendor/larvel/framework/Illuminate/Foundation/Exceptions/Handler.php
You are overriding what you are doing in the parent class in the .


2022-09-30 18:15

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.