Thin controllers in Laravel 4

I am trying to develop the β€œbest” way to structure my controllers / models in a new Laravel 4 application.

Obviously, I want the controllers to be thin and light. So I want to work with Repos / Services to separate things, however I really don't know how to implement this in Laravel 4. Laravel 3 gave us some idea about how this should work , but there are no samples.

Has anyone figured out which way to do this, or is there some example code that I could take a look at?

+4
source share
3 answers

I agree that it is not clear where to store these classes in Laravel 4.

A simple solution would be to create repository / service folders in your main application / folder and update the main composer.json file to load them automatically:

{ "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: every time you create a new class, you need to run composer dump-autoload .

In the case of repositories, you can force Laravel to automatically enter them into your controllers. I find this good screencast on this.

+7
source

I found this collection of tutorials a great investment in the right place to store your own service providers and other files that you want to enter into your project:

These tutorials are taken from this collection:

http://fideloper.com/tag/laravel-4

+3
source

The first thing I do when I get a new Laravel installation:

  • Delete the "models" folder
  • Create src folder
  • In src create an application folder (or application name)
  • In the Application folder, create objects, repositories, services, and tests

So, in this case, your namespace will be Application \ Entities, Application \ Repositories and Application \ Services

In composer.json:

 { "require": { "laravel/framework": "4.0.*" }, "autoload": { "psr-0": { "Application": "app/src/" }, "classmap": [ "app/commands", "app/controllers", "app/database/migrations", ] }, "minimum-stability": "dev" } 

For each of them, you need to create classes and a service provider to bind the repository to the entity.

Here is a tutorial explaining how to create repositories:

http://culttt.com/2013/07/08/creating-flexible-controllers-in-laravel-4-using-repositories/

Anyway, I'm still looking for the best architecture. If anyone has a better idea, I will be happy to hear that!

+2
source

All Articles