Home > Backend Development > PHP Tutorial > yii20 custom global tool function

yii20 custom global tool function

WBOY
Release: 2016-07-28 08:26:50
Original
1036 people have browsed it
When we develop projects, we often use some test output methods, such as print_r(), var_dump(), etc. For these common methods, we can write a global tool function ourselves and encapsulate it uniformly. It is also convenient to use.

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; }
Copy after login

<span>    要是将此文件,在每个文件中引入又比较麻烦,所以,我们可以将其引入入口文件中,就可以全局使用了。</span>
Copy after login
<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">应用:
Copy after login
<?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);
    }

}
Copy after login

运行结果:
Copy after login
<pre class="brush:php;toolbar:false">Array
(
    [name] => ysy
    [age] => 21
)
Copy after login

       当然大家也可以根据自己需求,在里面写入其他的一些常用方法,加快项目开发速度。
Copy after login



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.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template