gpt4 book ai didi

yii - 全局注册 ClientScript 的最佳方式?

转载 作者:行者123 更新时间:2023-12-04 22:49:44 24 4
gpt4 key购买 nike

我想全局注册用户脚本,以便在整个站点上可用。现在我插入 Controller 中的每个 Action :

Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl . '/js/script.js');

但我真的明白这不是好方法......

最佳答案

如果您希望在您的项目中使用主题,我会在布局文件中放置一些 css 和脚本( views/layouts/my-layout-file.php )。因为如果您更改主题,您将使用另一个 css,有时可能还会使用另一个脚本,所以您不想将它们混合在一起。

但是一些主要的 css 和 scipts,没有改变跨主题,我会放在主要 Controller ( protected /组件/Controller.php)
和所有其他 Controller (/protected/controllers/)将扩展此类 Controller

class PageController extends Controller {

因此,如果您的所有 Controller 都在父类上使用,您可以只编辑父类并添加类似的内容
public function beforeRender( $view ) {
...
Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl . '/js/script.js');
...
return true;
}

现在您的所有操作都将使用相同的脚本。

编辑 :@realtebo(在评论中)指出使用' beforeRender ' 不是 'beforeAction'。

查看更多: Understanding the view rendering flow

关于yii - 全局注册 ClientScript 的最佳方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10025189/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com