【问题标题】:Type hinting parent::__construct() arguments in controllers在控制器中键入提示 parent::__construct() 参数
【发布时间】:2014-08-26 11:16:14
【问题描述】:

我在基于 Laravel 框架的应用程序中有一个 BaseController,代码如下:

class BaseController extends Controller {
    public function __construct(Credentials $credentials) {
      $this->credentials = $credentials;
}

然后,我所有的其他控制器都将扩展 BaseController:

class PostController extends BaseController {
public function __construct(PostRepository $post)
{
    $this->post = $post;
    parent::__construct();
}

但是,我需要在 parent::__construct(); 中键入提示 Credentials 类;我所有的控制器。有什么办法可以避免吗?

提前致谢

【问题讨论】:

    标签: laravel laravel-4 inversion-of-control ioc-container


    【解决方案1】:

    我可以使用以下代码解决它:

    class BaseController extends Controller {    
    
    public function __construct()
    {
        $this->credentials = App::make('Credentials'); // make sure to use the fully qualified namespace of Credentials if need be
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2010-12-08
      • 2019-11-02
      • 1970-01-01
      • 2018-01-19
      • 2019-07-13
      • 2018-12-04
      • 1970-01-01
      • 2020-03-26
      • 1970-01-01
      相关资源
      最近更新 更多