In Yii2.0, we can create a folder ourselves in the root directory of the project. Here we take helper as an example.
In the helper folder, create a function.php.
Write the following code in function.php:
<?php function p($var){ echo "<pre class="brush:php;toolbar:false">"; print_r($var); echo ""; } function dd($var){ echo "
"; var_dump($var); echo ""; die; }
<span> 要是将此文件,在每个文件中引入又比较麻烦,所以,我们可以将其引入入口文件中,就可以全局使用了。</span>
<span> 在web/index.php中,添加一行,引入此文件。 <img src="http://image.codes51.com/Article/image/20160713/20160713163735_2995.png" alt=" yii20自定义全局工具函数"></span><pre class="brush:php;toolbar:false">应用:
<?php namespace app\controllers; use Yii; use yii\web\Controller; class TestController extends Controller { function actionDatePicker(){ return $this->render("DatePicker"); } function actionIndex() { $data=[ 'name'=>'ysy', 'age'=>'21', ]; p($data); } }
运行结果:
<pre class="brush:php;toolbar:false">Array ( [name] => ysy [age] => 21 )
当然大家也可以根据自己需求,在里面写入其他的一些常用方法,加快项目开发速度。
The above introduces the yii20 custom global tool function, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.