5

I am going crazy looking for a solution to this. I need to do a very simple thing - pass variables to the custom 404 page layout. Laravel lets you easily create a custom view for your http errors by creating a file like /resources/views/errors/404.blade.php but why doesn't it easily let you pass variables to it?

I tried overwriting app/Exceptions/Handler.php render method:

public function render($request, Exception $e)
    {
        if($this->isHttpException($e)){
            switch ($e->getStatusCode()) {
                case '404':
                      parent::render($request, $e)->flash();
                      $categories = Category::hierarchy();
                      return View::make('errors.404')->with(['categories' => $categories]);
                break;

                default:
                    return $this->renderHttpException($e);
                break;
            }
        }
        return parent::render($request, $e);
    }

But for whatever reason this doesn't work and I cannot access the categories variable in my master layout. I'm working on a site that displays header on error pages but if I can't pass variables to the error view then my header cannot be created.

Undefined variable: categories

Anyone has an idea of what could be going wrong? Is it just impossible to do this? I have read you can pass the exception in and get a message from it but what's the point of that? I don't want to have to duplicate the entire layout and rewrite all variables.

Nikhil Vaghela
  • 2,088
  • 2
  • 15
  • 30
PeterTheLobster
  • 1,386
  • 16
  • 33

5 Answers5

1

i was creating custom error layout(views/error/404.blade.php), (had to pass variable to check if user was logged in or not). you can use something like this

@include('layouts.article', ['mainTitle' => "404, page not found", 'mainContent' => "sorry, but the requested page does not exist :("])

or like this

<?php $data=[
        'mainTitle' => "404, page not found",
        'mainContent' => "sorry, but the requested page does not exist :("
    ]  ?>
@include('layouts.article', $data)

reference: Laravel Blade passing variable with string through @include causes error

Sahil Kashyap
  • 329
  • 2
  • 10
0

Add

public function boot()
{
  view()->composer('my.view', function($view)
  {
    $view->with('myVariable', 'whatever you want');
  });
}

to the AppServiceProvider.php

This will pass whatever you want as a $myVariable to the view.blade.php in my folder. There might be typos.

More info: https://laracasts.com/series/laravel-5-fundamentals/episodes/25 (code at 9:32)

dbr
  • 1,037
  • 14
  • 34
0

You're looking for a View Composer: https://laravel.com/docs/8.x/views#view-composers

Andres Felipe
  • 4,292
  • 1
  • 24
  • 41
anakadote
  • 322
  • 2
  • 11
0

There some breaking change in 5.4, you should use :: as delimeter for error views, for example:

View::composer('errors::*', function($view){
    $view->with('varName', 'value');
})
Oleg Shakhov
  • 426
  • 6
  • 27
  • 1
    But this does not work when rendering views manually through view('404'), so you should use this: `View::composer(['errors::*', 'errors.*'], function ($view) {` – Maarten00 Feb 17 '22 at 15:43
0

This trick working for me.

In your controller method:

abort(404,json_encode($categories));

In your 404.blade.php:

@php $categories = json_decode($exception->getMessage()) @endphp

@foreach($categories as $category)...
RichardMiracles
  • 2,032
  • 12
  • 28
  • 46