This article describes the method of adding custom helper functions in the Laravel5 framework. Share it for your reference, as follows:
Laravel contains many very useful helper functions, such asarray_get()
、array_first()
、app_path()
Wait, please check it out/post/Documentation, these helper functions provide us with many simple and easy-to-use functions, which improves our development efficiency, but what if we want to add a customized helper function? It's actually very easy, and it can be done in four steps:
1. Create custom helper functions
Here we put the functionapp/Support/Helpers/Inside:
<?php if (! function_exists('test_function')) { function test_function() { echo "I'm a custom helper function"; } }
2. Loading of auxiliary function files
Create a fileapp/Support/Helpers/, and load the file containing the custom functions:
<?php $helpers = [ '' ]; // Loadforeach ($helpers as $helperFileName) { include __DIR__ . '/' .$helperFileName; }
3. Automatically load files in
/**/ { "autoload": { "classmap": [ "database" ], "psr-4": { "App\\": "app/" }, "files": [ "app/Support/Helpers/" ] } }
4. Recompile the file
Run the following command:
composerdump-autoload
After running, you can call your custom functions anywhere, it's that simple.
For more information about Laravel, readers who are interested in view the topic of this site:Laravel framework introduction and advanced tutorial》、《Summary of excellent development framework for php》、《PHP object-oriented programming tutorial》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php》
I hope that this article will be helpful to everyone's PHP programming based on the Laravel framework.