1024programmer PHP yii1.4constant.php, Yii2 alias is simple to use

yii1.4constant.php, Yii2 alias is simple to use

First of all, what is an alias. There are many paths in Yii , We will also use some paths during the development process. Generally speaking, you need to use absolute paths,but absolute paths are very long. So , for the convenience of using paths, you can give each path a name in Yi , this name is an alias.

Look directly at the example :

The first usage :

First set an alias :Yii::setAlias( & # 39; & # 64; ww & # 39;, & # 39; 123 & # 39;);

Now you can get : echo Yii::getAlias(& # 39; & #64;ww');//The result is 123

If you did not write the ‘@’ symbol :echo Yii::getAlias('ww& #39;);//The result is the direct output of the ww string

The second usage :

As mentioned earlier, the alias of , is for the convenience of using the path ,Then look at specific examples:

For example ,Your project often uses some frequently used constants,You can create a new constant under config .php,

constant.php Code:

When you , want to use this constant in the controller, you must import this file

You can write like this: include_once(realpath(dirname(__FILE__).'../../config')).'/constant.php';

Or use an alias to introduce: include_once(Yii::getAlias(“@app/config/constant.php”));

The third usage :

This usage is in the second optimization, we can write a sentence in the configuration file (web.php) (note that :aliases and components are at the same level,do not Write the following code into components)’aliases’=>['@ww'=>dirname(__FILE__).'/constant .php',],

Also write include_once(Yii::getAlias('@ww')); in the controller as well

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/yii1-4constant-php-yii2-alias-is-simple-to-use/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索