Home > PHP Framework > Laravel > body text

How to use the introduction of custom public functions in laravel

步履不停
Release: 2019-07-27 09:30:44
Original
2966 people have browsed it

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

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"
        ]
Copy after login

3. Execute the composer command

Open the terminal and execute the following command:

composer dump-auto
Copy after login

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

★ 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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!