Adding global function in Laravel using Composer
We use functions in our projects by importing and name spacing it in a class, which is only accessible in that class. In this blog we will see how to use a global function which can be used anywhere in the entire project. You can use a global function in any of the class, Controller or View as it requires. First you need to create a Helpers folder in your projects app directory. In that Helpers folder create a php file, lets call it your_helper_function.php. Now write your function inside the your_helper_function.php file.
function your_function($parameters){
//function logic
}
function your_another_function($parameters){
//function logic
}
Now, in-order to make this function accessible throughout the project add this file to Files key in the composer.json file.
"autoload":{
...
"files":[
"app/Helpers/your_helper_function.php"
]
...
}
Lastly run composer dump autoload command in you project directory.
composer dump-autoload
Comments
0 comments
Please Sign in or Create an account to Post Comments