When searching for the right way to add a simple Helper class in Laravel 5, I’ve come across several suboptimal solutions. Some advice to create a folder and then glob() all files and incrementally require or include them. There is an easy way and you don’t have to rewrite any code.
In your app/ folder, create a folder named Helpers. Now in this folder, add a Helper.php file. Now here’s the trick: With namespacing, you can reference your class without any other hassle. Simple include the following at the top of your file:
namespace App\Helpers; /* if you named your folder differently than Helpers, use your folder name! */ /* add class now */ class ClassName { .... }
And that’s it. You can now use your Helpers class anywhere in your code simply by calling \App\Helpers\ClassName::doSomething().
Of course, if you would like to make it easier to access the function only with ClassName you can add a class alias in the config/app.php file in the aliases array. Simply add your alias and full referenced class like so:
'ClassName' => \App\Helpers\ClassName::class
I hope this is helpful and avoids any glob() headache. Happy coding!