SoFunction
Updated on 2025-04-04

Implement encapsulated public method global call in laravel framework

1. Create

Create a new file under app/Helpers/ (you can do the directory by yourself), and add the following code internally:

<?php
/**
  * Public method Returns json data and prompts for information
  * @param $status status
  * @param string $message prompt information
  * @param array $data Return data
  */
function showMsg($status,$message = '',$data = array()){
  $result = array(
    'status' => $status,
    'message' =>$message,
    'data' =>$data
  );
  exit(json_encode($result));
}

2. Configuration

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

"files":[
      "app/Helper/"
    ]

3. Execute the composer command

Open the terminal and execute the following command:

composer dump-autoload

4. Test

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

showMsg(1,'Hello World!');

The above article implements the global call of encapsulated public methods in the laravel framework is all the content I have shared with you. I hope you can give you a reference and I hope you can support me more.