简体   繁体   English

Laravel 3:错误响应未扩展基本控制器(定义资产的地方)

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

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容器“标头”,例如:

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 在我的master.blade.php中

However, when a 404 is triggered 但是,当触发404时

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. 我认为这是因为laravel错误控制器没有扩展基本控制器。

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. 我以为在刀片式服务器布局中声明我的所有资产并不是最佳方法。

Ah ha! 啊哈!

I've just worked it out. 我已经解决了。

The answer is to use View Composers (one of the Laravel concepts I never properly investigated..) 答案是使用View Composers (我从未适当研究过的Laravel概念之一。)

So in my routes.php (temporary placement - I should probably place composers in their own auto-loaded file) I've just added the following. 因此,在我的routes.php中(临时放置-我可能应该将作曲家放置在自己的自动加载文件中),我刚刚添加了以下内容。

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. 鉴于我拥有的每个前端文件都在使用/views/layout/master.blade.php,那么我知道可以通过将'layout.master'传递给View :: composer来对其应用资产。

Et voila - now my 404 view is also loading the correct assets. 等等-现在我的404视图也正在加载正确的资产。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM