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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With