我在基于Laravel Framework的应用程序中有一个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();
}
但是,我需要在父:: __ construct()中键入提示Credentials Class;我的所有控制器有没有办法避免这种情况?
提前致谢
答案 0 :(得分: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
}
}