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 = '',$data = array()){ $result = array( 'status' => $status, 'message' =>$message, 'data' =>$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,'Hello World!');
★ 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!