Laravel 5.0 Application Structure Laravel 5.0 Application Structure laravel laravel

Laravel 5.0 Application Structure


Your code looks great and well thought to generalisation.

I would like to suggest this option for using a Factory for creating your services, and not predefine all the dependency injections.

interface UpdateServiceInterface {    public function action($itemID);    //....}class ThingUpdateServiceInterface implements UpdateServiceInterface {    public function action($itemID)    {        // TODO: Implement exists() method.    }}class ApiServiceFactory {    protected $app;    public function __construct(Application $app) {        $this->app = $app;    }    public function getUpdateService($type) {        if ($type == 'things')            return $this->app->make('ThingUpdateServiceInterface');    }   public function getCreateService($type) {        if ($type == 'things') {            //same idea here and for the rest        }    }}class ApiController {    protected $factory;    protected $model;    public function __construct(ApiServiceFactory $factory) {        $this->factory = $factory;    }    public function update($data) {        $updater = $this->factory->getUpdateService($this->model);        $updater->action($data);    }}class ThingsController extends ApiController {    protected $model = 'App\Thing';}

what is the idea of all of this is:

  • basic ApiController that holds all the methods: update, insert, delete...
  • each other controller for specific object will extend the api controller and override the $model with the Model full name.
  • in the action of the controller it uses the factory for creating the service for that specific object.
  • after creation of the service it uses it for the action desired.
  • you could create general action for the action, like

    DeleteServiceInterface

will be implemented by

    class GeneralDeleteService implements DeleteServiceInterface {           public function delete($id){......}    }

and in the factory when you request for that DeleteService if no name is passed so you will return the default service

hope this post wont be "TLDR"