How to Create Helper Methods on Laravel Not a Facade

How to create Helper Methods on Laravel not a Facade

If you want to go the 'Laravel way', you can create helpers.php file with custom helpers:

if (! function_exists('myCustomHelper')) {
function myCustomHelper()
{
return 'Hey, it\'s working!';
}
}

Then put this file in some directory, add this directory to autoload section of an app's composer.json:

"autoload": {
....
"files": [
"app/someFolder/helpers.php"
]
},

Run composer dumpauto command and your helpers will work through all the app, like Laravel ones.

If you want more examples, look at original Laravel helpers at /vendor/laravel/framework/Illuminate/Support/helpers.php

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"
]

How to use DB:: from helper class

Change your using to: Illuminate\Support\Facades\DB;

Laravel - performance of Facades vs. helper methods

I don't think there is much performance difference but one thing to consider is the reduced cognitive load of always including the use statement when using a facade. Just one more thing to forget.

Why create a facade in laravel instead of calling a method directly?

A Facade in Laravel is only a convenient way to get an object from the Service Container and call a method on it.

So calling a Facade like this :

//access session using a Facade 
$value = Session::get('key');

Is like doing:

//access session directly from the Service Container
$value = $app->make('session')->get('key');

As the Facade resolves the session key out of the Service Container and call the method get on it

Once understood what a Facade does, you should understand what is the Service container and what are the benefits of using it

The Service Container in Laravel cloud be a Dependency Injection Container and a Registry for the application

The advantages of using a Service Container over creating manually your objects are stated in one of my previous answers and in the doc page, but briefly:

  • Capacity to manage class dependencies on object instantation
  • Binding of interfaces to concrete classes, so that when a interface is requested in your program, a concrete class is instantiated automatically by the service container. Changing the concrete class on the binding, will change the concrete objects instantiated through all your app
  • Possibility to create single intances and get them back later (Singleton)


Related Topics



Leave a reply



Submit