简体   繁体   中英

Laravel Service as Controller - working with multiples controllers

I'am a Brazilian developer, so... sorry for my limited English right away.

Well, in fact my problem is more a convention problem because until now I hadn't use services with Laravel (my apps were that simple so far).

I read about it before ask this question, but nothing helped with this specific situation. I'll try to describe in a objective way.

before that, just a comment: I know about the mistake using just controllers in these example. The ask is really about that mistake.

Well, the actual structure is:

abstract class CRUDController extends Controller {

    protected function __construct($data, $validatorData) {
        // store the data in a attribute
        // create with Validator facade the validation and store too
    }

    abstract protected function createRecord();

    protected function create() {
        try {
            // do the validation and return an Response instance with error messages
            // if the data is ok, store in the database with models
            // (here's where the magic takes place) in that store!
            // to do that, calls the method createRecord (which is abstract)
            $this->createRecord();
            // return a success message in an Response instance
        }
        catch(\Exception $e) {
            // return an Response instance with error messages
        }
    }

}

class UserController extends CRUDController {

    public function __construct($data) {
        parent::__construct($data, [
            'rules' => [
                 // specific user code here
             ],
            'messages' => [
                 // specific user code here
             ],
            'customAttributes' => [
                 // specific user code here
             ]
        ]);
    }

    protected function createRecord() {
        $user = new UserModel();
        // store values here...
        $user->save();
        return $user;
    }

}

// here's the route to consider in that example
Route::post('/user', 'WebsiteController@register');

class WebsiteController extends Controller {

    private $request;

    public function __construct(Request $request) {
        $this->request = $request;
    }

    public function register() {
        $user = new UserController();
        $user->create($this->request);
        // here's the problem: controller working with another controller
    }

}

class UserAPIController extends Controller {
    // use here the UserController too
}

and many other classes that extends CRUDController in the same way...

What I want

I want to create a controller (called here as CRUDController) to reuse methods like the pattern says (create, read, update and delete). To be really objective here I'll use the create method as an example. With the code above it seems clear the purpose? I think so... all my controllers have that code of validation equal and reusable. That's the thing. Besides that, I want to my route of website call another controller (UserController) to store new users... but in the same way, I'll create an API that uses the same controller in the same way (with validations etc). That's the purpose of Responses in the CRUDController (I'll read them in the WebSiteController to resolve what to do, like show a view and in the other hand with the API I'll basically return the Response.

My real problem

Convention and pattern. The MVC pattern is broken here. Controller calling another controller is wrong and I know that. I want to know what thing I should use! Services? Is that right? I see a lot (really) of examples of services but nothing like that, working with models and reusing code, etc. I never use Services but I know how to use, but I don't know if it's right to these cases.

I really hope that someone can help here and sorry once again for the mistakes with the English. Thanks a lot.

You're calling the CRUD controller a controller but it does not behave as an MVC controller. At best it's just a helper class. You could always do this:

abstract class CRUDManager {        
    //As you had the CRUDController
}

class UserManager extends CRUDManager {
     //As you had the UserController
}

In your AppServiceProvider:

public function boot() {
     $app->bind(UserManager::class, function ($app) {
           return new UserManager(request()->all()); //I guess that's what you need.
     });
}

Whenever you need to use it you can do:

public function register(UserManager $user) {
    $user->create();
}

Now one thing to point out. It's not a good idea to initialise the request in the constructor. You should use dependency injection in controller methods. I don't even know if the request is available when the controller is being constructed (I know the session is not). The reason why I say this is that the middleware runs after the controller is constructed and therefore the request may be modified when the controller method is called.

Another note: If you did the original solution because you needed to use certain controller methods, then you can just use the corresponding traits (because the controller itself does not really have many method). I'm guessing a trait like ValidatesRequests would be one you'd need to use .

I'll answer my own question. I use a pattern called Repository Pattern to resolve the problem (or I try to use, because it's the first time using this pattern: maybe I don't use in the right way in every steps).

Files structure

Controllers
  UserController.php
Models
  UserModel.php
Providers
  UserRepositoryServiceProvider.php
Repositories
 RepositoryInterface.php
 Repository.php
 User
   UserRepositoryInterface.php
   UserRepository.php
Traits
  InternalResponse.php

With that structure I did what I wanted in my question without working just with controllers.

I create a trait called InternalResponse. That trait contains a few methods that receive a transaction, validate if it's the case and then return a Response (called "internal" in my logic because the controller will read and maybe change the Response before return it in the end).

The Repository class, which is abstract (because another class must extend it to make sense to use. In this case the class UserRepository will extend...), uses the Trait mentioned.

Well, with it in mind, it's possible to know that the UserController uses the UserRepositoryInterface, that provides an object UserRepository: because the UserRepositoryServiceProvider register this with that interface.

I think there's no need to write code here to explain, because the problem is about an pattern, and these words explain well the problem (in the question) and the resolution with this answer here.

I'll write here a conclusion, I mean, the files structure with comments to explain a little bit more, to end the answer.

Conclusion: Files structure with comments

Controllers
  UserController.php
    // the controller uses dependency injection and call methods of 
    // UserRepository, read and changes the Response receveid to finally
    // create the final Response, like returning a view or the response
    // itself (in the case it's an API controller)
Models
  UserModel.php
    // an normal model
Providers
  UserRepositoryServiceProvider.php
    // register the UserRepositoryInterface to
    // return a UserRepository object
Repositories
 RepositoryInterface.php
   // the main interface for the Repository
 Repository.php
   // the main repository. It's an abstract class.
   // All the others repositories must extend that class, because
   // there's no reason to use a class Repository without an Model
   // to access the database... That class share methods like create,
   // read, update and delete, and the methods validate and transaction
   // too because uses the trait InternalResponse.
 User
   UserRepositoryInterface.php
     // the interface for UserRepository class
   UserRepository.php
     // that class extend Repository and uses the UserModel
Traits
  InternalResponse.php
    // trait with methods like validate and transaction. the method
    // validate, read and validate the data receveid for the methods
    // create and update. and all the CRUD methods uses the method
    // transaction to perform the data to the database and return a
    // response of that action.

That's what I do and like I said before, I don't know if it's a hundred percent correct in reference to Repository Pattern.

I hope this can help someone else too. Thanks for all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM