Home » Php » Auto-loading Helpers in laravel 4

Auto-loading Helpers in laravel 4

Posted by: admin November 29, 2017 Leave a comment


I’ve been trying to get Laravel 4 to auto-load helper files from the app/helpers directory (I created this, obviously).

I started by doing it the Composer way: add the path to composer.json, and then running dump-autoload. This did not work. I then tried with the app/start/global.php file, which did not work either.

Note, I am not throwing classes into the helper files – that’s what Facades and Packages are for. I only need small helper functions, similar to those of Laravel’s own (in the vendor directory). I only say this because it seems that composers dump lists classes (with namespaces) only.

What can I do to get the helpers to auto-load?


It also seems that the ClassLoader::addDirectories() function does not work for classes – why is it there then? Do I have to use both this and Composer?


It seems my question is not being understood. In my app/helpers directory, I have a file called paths.php. I want to be able to call a function within it (theme_path($location)) within the global scope.


Autoloading is only for classes, you can’t really autoload random php files based on one of their function names.

Your best bet is go OOP and use classes for your helpers. You’ll gain the efficiency of autoloaded files only when needed, and you’ll be closer to what the rest of the Laravel community does.

But if you still want to use plain old non-object php functions, I guess you could simply individually require() all of them from start.php, but they’d all be loaded on every page request.


You need to do the following:

"autoload": {
    "files": [
        "file location go here"

Then you’ll be able to use the functions in the helper file. However it’s worth noting that this will load the file explicitly on every request. See http://getcomposer.org/doc/04-schema.md#files for more details.

I would recommend that unless this is a heavily used file of helpers, avoid doing this and just organise it with namespaces and classes to avoid naming collisions.


Well… I can show you my composer and helpers to show how I made it to work:

"autoload": {

    "psr-0": {
        "Helpers": "app/libraries"

And then in app/libraries/Helpers/DateHelper.php (for instance)

<?php namespace Helpers;

class DateHelper extends \DateTime {


After creating the helper file I just run the

composer dump-autoload

And now just use your helper as Helpers\DateHelper


Not directly related to your question, but Laravel has integrated Carbon library (https://github.com/briannesbitt/Carbon) for dates handling. It’s inside vendor/nesbot folder, so you can use it directly in Laravel.