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.)

[ps]cd app/

mkdir library

cd library[/ps]

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

[ps]cd ..

vim composer.json[/ps]

Should look something like.

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

We're going to add the library directory.

[ps]{
"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"
}[/ps]

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

[ps]composer dump-autoload[/ps]

Now you can use the library folder.