Home  >  Article  >  Backend Development  >  yii2 如何自定义函数?存放在哪里比较好

yii2 如何自定义函数?存放在哪里比较好

WBOY
WBOYOriginal
2016-06-06 20:38:09879browse

Yii2 中如何自定义全局函数,目的用于生成网址!存放位置放哪里最合适?
这些自定义的自定义函数会在action中和views中用到


2015-2-9 补充:

我的方法:

<code>php</code><code>#app\helpers\myUrl.php
namespace app\helpers;

class myUrl {
}
</code>

回复内容:

Yii2 中如何自定义全局函数,目的用于生成网址!存放位置放哪里最合适?
这些自定义的自定义函数会在action中和views中用到


2015-2-9 补充:

我的方法:

<code>php</code><code>#app\helpers\myUrl.php
namespace app\helpers;

class myUrl {
}
</code>

放到 helpers 里面是对的。使用静态方法

之前我也为这个事情惆怅了好几天,感觉要是随便放放就破坏了YII的机制,后来我想了很久,发现可以丢在组件里,就是在项目下,新建components文件夹,然后新建helpers文件夹,用来放自己的帮助类,静态方法是对的!除了些帮助类方法以为,还有写Utility这种的函数,可以放在项目\common\includes\xxcommon\includes\xxUtility.php

我的做法是把这些方法封装到一个类里面,用类的静态方法的形式去使用。

还可以放到 bootstrap.php 文件中, 位于 advanced-template 的配置文件夹内

能不能这样,在config下建一个php文件,放全局函数。

1.新建一个php文件夹在config下

yii2 如何自定义函数?存放在哪里比较好

2.在项目入口处index.php引用

yii2 如何自定义函数?存放在哪里比较好

但是这样的话感觉不太规范,配置文件夹应该只放配置相关的文件。求yii开发项目中更好的做法,谢谢。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn