Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto-loading Helpers in laravel 4

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?

Update

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?

Edit

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.

like image 628
Mike Rockétt Avatar asked Dec 15 '22 09:12

Mike Rockétt


1 Answers

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.

like image 174
Safeer Avatar answered Dec 28 '22 13:12

Safeer