I'm trying to understand the best place to put a global function in Laravel 4. For example, date formatting. I don't think making a facade is worth it as facades are too modular. I've read articles about creating a library folder and storing classes there but that also seems like a lot for a simple function. Shouldn't a 'tool' like this be available in Blade templates?
What are the best practices for something like this and how do I make it available to Blade templates?
My way of doing this is to create a new folder in the
/app
directory in the root of your Laravel 4 project. Then add this folder to the first array of the/app/start/global.php
file like so:As long as the folder structure within the new
/app/classes
folder follows your namespacing convention. Laravel 4 will autoload all the classes/files within this folder. This way there's no need to dig into any composer files or run composer command.Not sure if this is best practice but it certainly works.
If you created a simple file called
/app/classes/Helpers/Helper.php
such as this:All you would need to do is call
Helpers\Helper::helloWorld();
You could also alias this helper class in your
/app/config/app.php
file. Just add something like this to the end of thealiases
array:You can also use
View::share()
together with closures to achieve this - I just posted about this: http://www.develophp.org/2014/07/laravel-4-blade-helper-functions/Added benefit: You don't need to create an extra class and also keep the global namespace clean.
Laravel's helpers.php method is to add it to your "files" in composer.json (https://github.com/laravel/framework/blob/master/composer.json):
What I do is to create small classes (a few methods per class, one line per method, everything extended from something and DRY, that's my goal),
save them to them in app/libraries and add to composer.json:
Execute
And then just use them wherever you need
Watch this video about refactoring: http://www.youtube.com/watch?v=DC-pQPq0acs
By the way, I'm kind of sure it was just an example, but you might not need a helper to format dates, since all dates in Laravel are instances of Carbon (https://github.com/briannesbitt/Carbon) and it has loads of methods to format date and time.
The ugly, lazy and awful way: At the end of
bootstrap/start.php
, add aninclude('tools.php')
and place your function in that new file.The clean way: Create a library. That way it'll be autoloaded ONLY when you actually use it.
libraries
folder inside yourapp
folderstart/global.php
to addapp_path().'/libraries'
to theClassLoader::addDirectories(
array.composer.json
to add"app/libraries"
to theautoload
array. Runcomposer dump-autoload
About your options, quoted from the
global.php
fileYou can combine both options, where the Laravel class loader will automatically search for classes in the registered directories (Option 1, easier) and Composer will keep record of all the classes but only after you update it (Option 2, might improve performance).