Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thin Controllers in Laravel 4

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?

like image 675
Tom Green Avatar asked Dec 15 '22 14:12

Tom Green


1 Answers

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.

like image 190
Ninja2000 Avatar answered Dec 27 '22 03:12

Ninja2000