I used to use CodeIgniter. In CodeIgniter whenever I had to create my own classes, I used to create a class in the library folder and used it as a library. In Laravel they use packages. In the documentation they also talk about creating your own packages. Are packages in Laravel 4 same as libraries in the CodeIgniter?
What seems to be common now is to put custom classes, exceptions, validation, etc. into a folder that is named in correspondence to your application.
For example:
| laravel
| -- AppName/
| -- Exceptions
| -- Repositories
| -- Validation
| -- CustomClasses (your custom class name)
| -- controllers
| -- models
| -- views
To include in composer, the easiest way is to include the classmap
composer.json
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php",
"app/AppName"
]
}
If you would like to use psr-4 auto loading instead, there is a great free video on Laracasts to get you started https://laracasts.com/lessons/psr-4-autoloading
Also, if you're a Laracasts member, the Where Do I Put This video is exactly what you're looking for with this question. https://laracasts.com/lessons/where-do-i-put-this
Laravel's packages are third-party libraries from Composer.
Your own classes that don't fit in a controller/model can go wherever you want as long as you tell the autoloader where to find them. We use app/libraries
and the PSR namespacing conventions. If you do so, you need to add app/libraries
to the composer.json
in the autoload-->classmap
array.