Home  >  Article  >  PHP Framework  >  How to use the introduction of custom public functions in laravel

How to use the introduction of custom public functions in laravel

步履不停
步履不停Original
2019-06-29 14:19:282920browse

How to use the introduction of custom public functions in laravel

Background

If you are used to using the ThinkPHP framework, having a public method class will be much faster in code writing, so it is necessary to do it here Configure it.

Testing framework: Laravel 5.5

Step guidance

1. Create functions.php

Create a new file functions.php under app/Helpers/ (you can choose the directory yourself), and add the following code internally:

<?php
/**
 * Created by PhpStorm.
 * User: moTzxx
 * Date: 2017/12/28
 * Time: 17:47
 */
/**
 * 公用的方法  返回json数据,进行信息的提示
 * @param $status 状态
 * @param string $message 提示信息
 * @param array $data 返回数据
 */
function showMsg($status,$message = &#39;&#39;,$data = array()){
    $result = array(
        &#39;status&#39; => $status,
        &#39;message&#39; =>$message,
        &#39;data&#39; =>$data
    );
    exit(json_encode($result));
}

2. Configure composer.json

Open the project In the composer.json file in the root directory, find the "autoload" configuration item and add the following code:

"files":[
            "app/Helper/functions.php"
        ]

3. Execute the composer command

Open the terminal and execute the following command:

composer dump-auto

4. Test

Execute the following code in any method of the controller. If there is data output, the configuration is successful:

showMsg(1,&#39;Hello World!&#39;);

★ Apply inferences to other public functions in the future. Written in functions.php...

For more Laravel related technical articles, please visit the Laravel Tutorial column to learn!

The above is the detailed content of How to use the introduction of custom public functions in laravel. For more information, please follow other related articles on the PHP Chinese website!

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