I am trying to work out the 'best' way of structuring my controllers/models in a new Laravel 4 app.
Obviously I want to keep the controllers thin and lightweight. So I want to work with Repos/Services to separate things, however I don't really know how to implement this in Laravel 4. Laravel 3 gave us some idea of how this should work, but no samples.
Has any one figured out the neatest way to do this, or have any sample code I could take a peek at?
I agree on the fact that it isn't very clear where to store these classes in Laravel 4.
A simple solution would be creating repositories/services folders in your main app/ folder and updating your main composer.json file to have them autoloaded:
{
"require": {
"laravel/framework": "4.0.*"
},
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/tests/TestCase.php",
"app/repositories",
"app/services"
]
},
"minimum-stability": "dev"
}
Note: everytime you create a new class you have to run composer dump-autoload
.
In case of repositories, you can have Laravel inject them into your controllers automatically. I find this a good screencast on this subject.
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