有什么方法可以在Phalcon的每个页面中添加资产? [英] Is there some way to add an asset to every single page in Phalcon?

查看:132
本文介绍了有什么方法可以在Phalcon的每个页面中添加资产?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读有关Phalcon的文档,试图找到一种利用资产的方法管理器,默认情况下将CSS或JS文件添加到每个页面.我一直没有找到执行此操作的任何方法.

I am reading the documentation for Phalcon attempting to find a method of utilizing the Assets Manager to add CSS or JS files to every single page by default. I have been unsuccessful in finding any method of doing this.

通过以下操作很容易为整个控制器添加资产:

Adding an asset for an entire controller is easily accomplished by doing:

public function initialize() {
    $this->assets->addCss('css/global.css');
}

  • 是否可以通过某种方式默认将资产添加到所有控制器?
  • 重写基本控制器类然后让我的控制器扩展的想法反而浮现在脑海,但这听起来有些草率,如果可能的话,我想避免这种情况.

    The idea of overwritting the base controller class and then having my controllers extend that instead comes to mind, but that sounds a little sloppy and I would like to avoid that if possible.

    谢谢,感谢您的帮助.

    推荐答案

    到目前为止,在基本控制器中执行此操作似乎是最好的方法,尽管我同意它确实有点草率.虽然感觉像css和js之类的资产被认为是视图权限的一部分,不应从其他任何地方接触,但在现实生活中,这不是事实.首先,您可以从控制器配置视图,指定要在其中输入哪些参数,并指定特定于视图的资产,等等.没有理由不向基本控制器中提供global.css的原因

    So far doing it in the base controller seems to be the best way, though I agree that it does smell a bit sloppy. While it feels that assets like css and js as supposed to be part of the view authority and shouldn't be touched from elsewhere, in real life this is not true. First of all, you configure your views from controllers, you specify what parameters go in there, and you specify individual view-specific assets, etc. There is no reason why you shouldn't provide the global.css with it in the base controller.

    我发现唯一真正草率的事情是有一个独立的视图和一个独立的资产管理器,实际上必须将它们耦合在一起,并且您在特定视图上设置的资产必须仅与该视图相关.在某些情况下,您希望独立于其他所有视图呈现视图并使用其自身的默认视图资源,例如在注册后呈现欢迎电子邮件模板.在当前的实现中,这只能通过重写大量逻辑来实现.

    The only thing that I find really sloppy is that there is a independent view and there is an independent asset manager, which in reality must be coupled together and assets you set on particular view must relate to that view only. There are cases when you want to render a view independently of everything else and use it's own assets from the default view, for example render the welcome email template after the registration. With the current implementation this can be achieved only by overwriting a fair amount of logic.

    或者,如果您不将样式缩小到单个文件中,则可以将其直接附加到模板中.您也可以尝试使用assets.addCss('css/global.css')直接在视图中添加资产–如果volt通过辅助函数执行此操作,则可能无法立即生效,在这种情况下,您应该首先实现自己的资产.但是,这意味着向视图中添加逻辑永远不是一件好事,除非您离不开它.因此,请坚持使用基本控制器方法.

    Alternatively you can attach that style directly in your template if you are not minifying them into a single file. You can also try adding an asset directly in your view with assets.addCss('css/global.css') – this might not work right away if volt does this via helper functions in which case you should implement your own first. This, however, implies adding logic to a view which is never a good thing unless you can't live without it. So, stick with the base controller approach.

    编辑:具有讽刺意味的是,一天后,在使用了一年多之后,我发现通过initialize()事件处理程序执行此操作时遇到了问题.

    ironically, a day later I discovered a problem with doing this via initialize() event handler after using it for over a year.

    当您从一个控制器转发到另一个控制器时,您转发到的控制器也会被初始化.这意味着同一资产被添加两次,并且如果将它们串联到一个文件中,上帝就会知道会发生什么……这对于css来说不是一个问题(只是大小增加了),但是JS很可能会破裂,通常没有任何迹象.通过在控制器中使用afterExecuteRoute()处理程序并进行调度程序状态检查,可以轻松解决此问题:

    When you forward from one controller to another, the controller where you forwarded also gets initialised. This means that the same asset gets added twice, and if they get concatenated into a single file god knows what's going to happen… This is less of a problem for css (only increased size), but JS is very likely to break, often without any indication. This gets easily fixed with using afterExecuteRoute() handler in controller with a dispatcher state check:

    public function afterExecuteRoute()
    {
    
        // If dispatcher hasn't finished dispatching we shouldn't enter.
    
        if (!$this->dispatcher->isFinished()) {
            return;
        }
    
        $this->assets->addCss('css/global.css');
    }
    

    在您的继承视图中,可以使用以下方法覆盖它:

    In your inheriting view just override this with this:

    public function afterExecuteRoute()
    {
        parent::afterExecuteRoute();
    
        if (!$this->dispatcher->isFinished()) {
            return;
        }
    
        $this->assets->addCss('css/custom.css');
    }
    

    这篇关于有什么方法可以在Phalcon的每个页面中添加资产?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆