簡體   English   中英

作為控制器的Laravel服務 - 使用多個控制器

[英]Laravel Service as Controller - working with multiples controllers

我是一名巴西開發人員,所以......對不起我有限的英語。

好吧,實際上我的問題更多是一個常規問題,因為直到現在我還沒有使用Laravel服務(到目前為止,我的應用程序非常簡單)。

我在問這個問題之前就讀到過這個問題,但這個具體情況沒有任何幫助。 我會嘗試以客觀的方式描述。

在此之前,只是一個評論:我知道在這些例子中只使用控制器的錯誤。 提問真的是關於那個錯誤。

那么,實際的結構是:

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
}

以及以同樣的方式擴展CRUDController的許多其他類......

我想要的是

我想創建一個控制器(在這里稱為CRUDController)來重用模式所說的方法(創建,讀取,更新和刪除)。 為了真正客觀,我將使用create方法作為示例。 有了上面的代碼,它的目的似乎很明確嗎? 我想是這樣的...我的所有控制器都具有相同且可重用的驗證代碼。 那是事情。 除此之外,我希望我的網站路由調用另一個控制器(UserController)來存儲新用戶......但同樣地,我將以相同的方式創建一個使用相同控制器的API(帶有驗證等)。 這就是CRUDController中響應的目的(我將在WebSiteController中讀取它們以解決要做的事情,比如顯示視圖,另一方面使用API​​我將基本上返回響應。

我真正的問題

公約和模式。 這里打破了MVC模式。 控制器調用另一個控制器是錯誤的,我知道。 我想知道我應該用什么東西! 服務? 是對的嗎? 我看到很多(真的)服務的例子,但沒有那樣,使用模型和重用代碼等。我從不使用服務,但我知道如何使用,但我不知道這些情況是否正確。

我真的希望有人可以在這里提供幫助,並再次對與英語的錯誤表示遺憾。 非常感謝。

您將CRUD控制器稱為控制器,但它不像MVC控制器那樣運行。 充其量它只是一個幫助類。 你總是可以這樣做:

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

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

在您的AppServiceProvider中:

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

無論何時需要使用它,您都可以:

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

現在有一點需要指出。 在構造函數中初始化請求不是一個好主意。 您應該在控制器方法中使用依賴注入。 我甚至不知道在構造控制器時請求是否可用(我知道會話不是)。 我之所以這么說是因為中間件在構造控制器之后運行,因此在調用控制器方法時可以修改請求。

另一個注意事項:如果你做了原始的解決方案,因為你需要使用某些控制器方法,那么你可以使用相應的特性(因為控制器本身並沒有真正的方法)。 我猜測像ValidatesRequests這樣的特性會是你需要use

我會回答我自己的問題。 我使用一種稱為Repository Pattern的模式來解決問題(或者我嘗試使用,因為它是第一次使用這種模式:也許我沒有在每個步驟中以正確的方式使用)。

文件結構

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

通過這種結構,我在我的問題中做了我想做的事情,而沒有使用控制器。

我創建了一個名為InternalResponse的特征。 該特性包含一些接收事務的方法,驗證是否是這種情況,然后返回響應(在我的邏輯中稱為“內部”,因為控制器將讀取並可能在最終返回之前更改響應)。

Repository類是抽象的(因為另一個類必須擴展它才有意義使用。在這種情況下,UserRepository類將擴展...),使用提到的Trait。

好吧,考慮到這一點,我們可以知道UserController使用UserRepositoryInterface,它提供了一個UserRepository對象:因為UserRepositoryServiceProvider在該接口上注冊了它。

我認為沒有必要在這里編寫代碼來解釋,因為問題是關於一個模式,這些詞很好地解釋了問題(在問題中)和這個答案的解決方案。

我將在這里寫一個結論,我的意思是,文件結構帶有注釋來解釋一點,結束答案。

結論:文件結構帶有注釋

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.

這就是我所做的,就像我之前所說的那樣,我不知道在參考存儲庫模式時它是否正確百分之百。

我希望這也可以幫助別人。 謝謝大家。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM