2017-10-29 212 views
2

Laravel鼓励依赖注入。由于我在我的项目中使用laravel,我想我会尝试使用这种方法。构造函数注入vs方法注入

我正在利用Laravel的服务容器type hinting my dependencies and letting it resolve them。我有四个控制器。所有这些扩展名为GlobalController的基类。我也有两个模型。所有这些扩展名为GlobalModel的基类。

我的第一次尝试是(有点)使用方法注入。 GlobalController如下所示:

namespace App\Http\Controllers; 

use Illuminate\Http\Request;; 
use App\Models\GlobalModel; 

class GlobalController extends Controller 
{ 

    public function __construct() 
    { 
     $this->middleware(['authenticate', 'token']); 
    } 

    // functions that handle normal http requests and ajax requests 

    } 

从GlobalController扩展的控制器之一称为UserController。它的一些功能是:

  • 指标 - 显示所有数据
  • 编辑 - 显示编辑表单
  • 更新 - 更新数据库

编辑和更新使用route-model-binding

namespace App\Http\Controllers; 

use Illuminate\Http\Request;; 
use App\Models\User; 

class UserController extends GlobalController 
{ 

    public function index(User $user) 
    { 
     $users = $user->all(); 
     return view('pages/view_users')->with('users', $users); 
    } 

    public function edit(User $user) 
    { 
     return view('pages/edit_user')->with('user', $user); 
    } 

    public function update(Request $request, User $user) 
    { 
     $data = $request->all(); 
     if ($user->validate($data)) 
     { 
      $user->update($data); 
      return $this->successResponse($request, 'users', 'Successfully edited user'); 
     } 
     return $this->failedResponse($request, $user); 
    } 

    // other functions 

    } 

虽然这工作正常,请求和用户注入很多次。如果必须更改Request实现(例如),我将不得不手动更改许多函数来键入提示该特定的Request对象。一点都不好。由于它们通常在大多数函数中调用,所以我尝试了构造函数注入。

下面是GlobalController使用构造器注入:

namespace App\Http\Controllers; 

use Illuminate\Http\Request;;; 
use App\Models\GlobalModel; 

class GlobalController extends Controller 
{ 
    protected $request; 
    protected $model; // use polymorphism 

    public function __construct(Request $request, GlobalModel $model) 
    { 
     $this->request = $request; 
     $this->model = $model; 
     $this->middleware(['authenticate', 'token']); 
    } 

    // functions that handle normal http requests and ajax requests 

} 

,这里是UserController中使用含有相同功能的构造器注入:

namespace App\Http\Controllers; 

use Illuminate\Http\Request;; 
use App\Models\User; 

class UserController extends GlobalController 
{ 

    public function __construct(Request $request, User $user) // use polymorphism 
    { 
     parent::__construct($request, $user); 
    } 

    public function index() 
    { 
     $users = $this->model->all(); 
     return view('pages/view_users')->with('users', $users); 
    } 

    public function edit(int $id) 
    { 
     $this->model = $this->model->find($id); 
     return view('pages/edit_user')->with('user', $this->model); 
    } 

    public function update(int $id) 
    { 
     $this->model = $this->model->find($id); 
     $data = $this->request->all(); 
     if ($this->model->validate($data)) 
     { 
      $this->model->update($data); 
      return $this->successResponse('users', 'Successfully edited user'); 
     } 
     return $this->failedResponse(); 
    } 

    // other functions 

} 

现在,我不能把我的手指上,但我认为这个实现看起来不正确。它变得不太可读。 $ model和$ this的使用使代码更加恶心。

我很困惑。我明白我可以从依赖注入中获得好处,但我确定我的方法注入和构造函数注入的实现是非常错误的。我应该选择什么样的实施?或者我应该从这两个中选择一个呢?

回答

1

我绝对更喜欢Laravel控制器的第一种方法。起初,你不需要在每种方法中注入模型。 (为什么要在索引函数中注入一个用户模型?)。

其次,您不能再使用RouteModelBinding的好处,而必须手动检查具有给定$ id的模型是否真的存在并采取相应措施。你也不能使用像CreateUserRequest这样可以处理验证和授权的特定FormRequests。(虽然这是一个可选功能)

另请注意,在构造函数中注入的模型从不是包含用户数据的“真实”模型。因此,这只会让您访问eleoquent功能。所以你可以在你的代码中使用User :: find($ id)。这总会让你失望。

public function __construct(User $user) 
{ 
    dd($user->exists); 
} 

如果你想抽象的东西,你可以在你的构造函数注入库。

public function __construct(UserRepository $userRepository) 
{ 
    $this->userRepository = $userRepository; 
    // then the Repository is responsible for retrieving users 
    // and you are not coupled to Eloquent. If you later want, you can Read 
    // users from an XML File if you need 
} 

附加信息(有点offtopic):虽然这是非常罕见的,我从来没有需要通过创建一个自定义的请求类这样的改变要求类,你可以这样做:

namespace App; 


use Illuminate\Http\Request; 

class MyRequest extends Request 
{ 
    // override request methods or add your new own methods 
} 

然后在全局index.php中:

$response = $kernel->handle(
    // instead of Illuminate\Http\Request::capture() 
    $request = \App\MyRequest::capture() 
); 
+0

“为什么要在索引函数中注入用户模型?”)。“ - 索引函数获取所有数据到视图。我需要$ user来做$ user-> all()。这不好吗? – morbidCode

+0

或只需调用User :: all()。如果您想要获取所有用户,则不需要特定的用户实例。或者如果您觉得这对您更好,请使用UserRepository。 –

+0

啊,你是对的。我可以使用外墙!但是,门面不做同样的事情?你的意思是说,在全部调用时,Facade不会创建新的User实例吗?我想我只是想尽量避免立面。 – morbidCode

1

如果在大多数方法中使用模型,请使用构造函数注入。 而且$ model和$ this的用法没有任何问题。但是,如果您仍想清除代码,请考虑存储库模式(SRP)。您可以管理那里的长长的代码。 看到这个stackoverflow answer - How can I organise classes in a Laravel 5 Project?。我希望这会帮助你的困惑。