나는 추상적 인 기본 컨트롤러 클래스를 가지고 있으며 모든 액션 컨트롤러는이 클래스에서 파생됩니다.기본 클래스와 파생 클래스의 종속성 주입
건설중인 기본 컨트롤러 클래스는 View 개체를 초기화합니다. 이 View 객체는 모든 액션 컨트롤러에서 사용됩니다. 각 액션 컨트롤러는 서로 다른 종속성을가집니다 (DI 컨테이너를 사용하여 해결됨).
문제는 기본 컨트롤러 클래스도 일부 종속성 (또는 매개 변수) 경로를 볼 폴더가 필요합니다. 그리고 질문은 - 기본 Controller 클래스에 매개 변수를 전달하는 위치와 방법은 무엇입니까?
$dic = new Dic();
// Register core objects: request, response, config, db, ...
class View
{
// Getters and setters
// Render method
}
abstract class Controller
{
private $view;
public function __construct()
{
$this->view = new View;
// FIXME: How/from where to get view path?
// $this->view->setPath();
}
public function getView()
{
return $this->view;
}
}
class Foo_Controller extends Controller
{
private $db;
public function __construct(Db $db)
{
$this->db = $db;
}
public function barAction()
{
$this->getView()->some_var = 'test';
}
}
require_once 'controllers/Foo_Controller.php';
// Creates object with dependencies which are required in __construct()
$ctrl = $dic->create('Foo_Controller');
$ctrl->barAction();
private $ view; $ view가 비공개 인 이유는 무엇입니까? 로드 할보기 경로는 언제 알 수 있습니까? 액션 컨트롤러 내부? 그러면 쉽게 할 수 있습니다. – busypeoples