How to create custom helper functions in Laravel
Create a helpers.php
file in your app folder and load it up with composer:
"autoload": {
"classmap": [
...
],
"psr-4": {
"App\\": "app/"
},
"files": [
"app/helpers.php" // <---- ADD THIS
]
},
After adding that to your composer.json
file, run the following command:
composer dump-autoload
If you don't like keeping your helpers.php
file in your app
directory (because it's not a PSR-4 namespaced class file), you can do what the laravel.com
website does: store the helpers.php
in the bootstrap directory. Remember to set it in your composer.json
file:
"files": [
"bootstrap/helpers.php"
]
Creating a custom helper in own package in laravel
Could not solve it by composer independently and used my package service provider.
I added this code to the boot()
method of my FaviconServiceProvider
and it works now:
if (File::exists(__DIR__ . '\app\helpers.php')) {
require __DIR__ . '\app\helpers.php';
}
How to make a custom helper function, available in every controller for Laravel 5
First create the required function inside the app
directory within a .php
file as
helpers.php
if (!function_exists('getServices')) {
public function getServices() {
return DB::table('services')->get();
}
}
and include this file in composer.json
inside autoload/files
array as
composer.json
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
},
"files": [
"app/helpers.php"
]
},
Then update the composer
, now you can able to directly use the created function inside your whole project as the file is automatically loaded when application get bootstraped
$result = getServices();
Custom helper classes in Laravel 5.4
I don't think it's possible to use only function when you have code in your classes. Well, you could try with extending Blade but it's too much.
What you should do is creating one extra file, for example app\Helpers\helpers.php
and in your composer.json file put:
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
},
"files": ["app/Helpers/helpers.php"] // <- this line was added
},
create app/Helpers/helpers.php
file and run
composer dump-autoload
Now in your app/Helpers/helpers.php
file you could add those custom functions for example like this:
if (! function_exists('fooBar')) {
function fooBar()
{
return \App\Helpers\CustomHelper::fooBar();
}
}
so you define global functions but in fact all of them might use specific public methods from some classes.
By the way this is exactly what Laravel does for its own helpers for example:
if (! function_exists('array_add')) {
function array_add($array, $key, $value)
{
return Arr::add($array, $key, $value);
}
}
as you see array_add
is only shorter (or maybe less verbose) way of writing Arr::add
Helper Function In Laravel
You should load the other files within the autoload
field.
{
"autoload": {
"psr-4": { ... },
"files": [
"app/Helpers/Helper.php"
]
}
}
https://getcomposer.org/doc/04-schema.md#files
Related Topics
How to Make Asynchronous Http Requests in PHP
Creating Default Object from Empty Value in PHP
How to Post Json Data With PHP Curl
Htaccess: Add/Remove Trailing Slash from Url
Recursive Function to Generate Multidimensional Array from Database Result
Why I Have to Call 'Exit' After Redirection Through Header('Location..') in PHP
Convert Multidimensional Array into Single Array
How to [Recursively] Zip a Directory in PHP
How to Make a Calculator in PHP
MySQL VS MySQLi When Using PHP
The Ultimate Clean/Secure Function
How to Close a Connection Early
Increasing the Maximum Post Size
Form Submit With Ajax Passing Form Data to PHP Without Page Refresh
How to Use Pdo Connection in Other Classes