Laravel 3: Error responses not extending base controller (where assets are defined)

158 views Asked by At

I might be approaching this the wrong way, but in my Base Controller I have defined, in the construct, my Asset container "header", eg:

Asset::container('header')->add('main-css', 'css/style.css');
// etc

As all my other controllers extend the base controller this is working fine and my assests are placed in the header container when I call

{{Asset::container('header')->styles()}}

In my master.blade.php

However, when a 404 is triggered

Event::listen('404', function()
{
    return Response::error('404');
});

My styles aren't loaded. I assume this is because the laravel error controller isn't extending the base controller.

Any easy way around this without redeclaring my assets or something. I was assuming that declaring all my assets in the blade layout wasn't the best approach.

1

There are 1 answers

0
alexleonard On BEST ANSWER

Ah ha!

I've just worked it out.

The answer is to use View Composers (one of the Laravel concepts I never properly investigated..)

So in my routes.php (temporary placement - I should probably place composers in their own auto-loaded file) I've just added the following.

View::composer(array('layout.master'), function($view)
{
    Asset::container('header')->add('bootstrap-css', 'bundles/bootstrapper/css/bootstrap.min.css');
    Asset::container('header')->add('main-css', 'css/style.css');

    Asset::container('footer')->add('bootbox', 'js/vendor/bootbox.min.js');

});

Given that every frontend file I have is using /views/layout/master.blade.php then I know that I can apply assets to it by passing 'layout.master' to View::composer.

Et voila - now my 404 view is also loading the correct assets.