[英]Laravel 4 Controller constructor
我只想知道是否可以在PostController中創建構造函數?
范例1:
public class PostController extends BaseController
{
protected $posts;
//Is this ok?
public function __construct(Post $posts){
$this->posts = $posts;
}
public function getPosts(){
$posts = $this->posts->all();
return View::make('get-index',compact('posts'))
}
}
可以在Controller中使用構造函數。 您可以利用Laravel的依賴注入和IoC容器,因此您的應用程序將更易於測試並且易於維護。
Taylor對DI和IoC容器做了很好的解釋。 您可以觀看以下視頻以獲取詳細信息:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.