How to add a library folder to Laravel 4

I typically use a library folder in my projects to group of files that you want to use in different projects but don’t want to use Satis to manage the contained files. This folder could also be a helper folder for functions that are static and are mainly generic helpers but don’t quite fit in a model. So in this example I’ll be adding a library folder and have it autoloaded by Laravel. The process for a helpers folder is the same, just replace library with helpers.

First step is to create the folder. (I assume you’re already in laravel project.)

cd app/

mkdir library

cd library

Now we need to add the folder to the autoload file. We will return to laravel project root and view the composer file.

cd ..

vim composer.json

Should look something like.

{
 "require": {
 "laravel/framework": "4.0.*"
 },
 "autoload": {
 "classmap": [
 "app/commands",
 "app/controllers",
 "app/models",
 "app/database/migrations",
 "app/tests/TestCase.php"
 ]
 },
 "minimum-stability": "dev"
 }

We’re going to add the library directory.

{
 "require": {
 "laravel/framework": "4.0.*"
 },
 "autoload": {
 "classmap": [
 "app/commands",
 "app/controllers",
 "app/library", /* Added here */
 "app/models",
 "app/database/migrations",
 "app/tests/TestCase.php"
 ]
 },
 "minimum-stability": "dev"
 }

Let’s reload the autoload. (Assuming composer is an alias.)

composer dump-autoload

Now you can use the library folder.

2 comments