0

如何使用 IoC 将 Sentry 2 User 模型注入 laravel 4 控制器?

例如我想要以下

class myController extends BaseController {

    /**
     * creates a list of MyModel Models
     *
     * @return View
     */
    public function getIndex( User $user )
    {
        // fetch models
        $models = MyModel::all();

        // Show the page
        return View::make('my-views.the-view', compact('models', 'user'));
    }

}
4

1 回答 1

1

这就是我喜欢这样做的方式:

class myController extends BaseController {

    $protected $user

    /**
     * creates a list of MyModel Models
     *
     * @return View
     */

    function __construct(User $user){
        parent::__construct();
        $this->user = $user;
    }

    public function getIndex()
    {
        // fetch models
        $models = MyModel::all();

        // Show the page
        return View::make('my-views.the-view', compact('models', 'user'));
    }

}

您也可以在该方法中执行此操作,但是...好吧,也请好好阅读:http: //fabien.potencier.org/article/11/what-is-dependency-injection

于 2013-10-18T09:26:45.693 回答