YII2框架自定义全局函数的实现方法小结
网络编程 2021-07-04 23:05www.168986.cn编程入门
这篇文章主要介绍了YII2框架自定义全局函数的实现方法,总结分析了YII2框架自定义全局函数相关实现技巧与操作注意事项,需要的朋友可以参考下
本文实例讲述了YII2框架自定义全局函数的方法。分享给大家供大家参考,具体如下:
有些时候我们需要自定义一些全局函数来完成我们的工作。
方法一:
直接写在入口文件处
<?php // ment out the following two lines when deployed to production defined('YII_DEBUG') or define('YII_DEBUG', true); defined('YII_ENV') or define('YII_ENV', 'dev'); require __DIR__ . '/../vendor/autoload.php'; require __DIR__ . '/../vendor/yiisoft/yii2/Yii.php'; $config = require __DIR__ . '/../config/web.php'; //自定义函数 function test() { echo 'test ...'; } (new yii\web\Application($config))->run();
方法二:
在app下创建mon目录,并创建functions.php文件,并在入口文件中通过require引入。
<?php // ment out the following two lines when deployed to production defined('YII_DEBUG') or define('YII_DEBUG', true); defined('YII_ENV') or define('YII_ENV', 'dev'); require __DIR__ . '/../vendor/autoload.php'; require __DIR__ . '/../vendor/yiisoft/yii2/Yii.php'; //引入自定义函数 require __DIR__ . '/../mon/functions.php'; $config = require __DIR__ . '/../config/web.php'; (new yii\web\Application($config))->run();
方法三:
通过YII的命名空间来完成我们自定义函数的引入,在app下创建helpers目录,并创建tools.php(名字可以随意)。
tools.php的代码如下:
<?php //注意这里,要跟你的目录名一致 namespace app\helpers; class Tools { public static function test() { echo 'test ...'; } }
然后我们在控制器里就可以通过命名空间来调用了。
<?php namespace app\controllers; use yii\web\Controller; use app\helpers\tools; class IndexController extends Controller { public function actionIndex() { Tools::test(); } }
更多关于Yii相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》及《》
希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程