在Yii2中为模块中的所有视图注册资产? [英] Register an asset in Yii2 for all views in a module?

查看:67
本文介绍了在Yii2中为模块中的所有视图注册资产?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Yii2中有一个模块,其中包含许多控制器,模型和视图.

I have a module in Yii2 containing a lot of controllers, models and views.

如何为所有视图注册资产,而又不一一地在所有视图中注册资产?

How can I register an asset for all views, without register it in all view one by one?

推荐答案

module 具有 init( )方法,您可以将其用于需要在每次访问模块时执行的代码:

The module has init() method, you can use it for code that needs to be executed every time the module is accessed:

<?php

namespace frontend\modules\users;

use frontend\assets\UsersAsset;
use Yii;
use yii\base\Module as BaseModule;

class Module extends BaseModule
{
    /**
     * @inheritdoc
     */
    public $controllerNamespace = 'frontend\modules\users\controllers';

    /**
     * @inheritdoc
     */
    public function init()
    {
       UsersAsset::register(Yii::$app->view);       

       parent::init();            
    }
}

别忘了调用父实现.

这篇关于在Yii2中为模块中的所有视图注册资产?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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