php 배우고있는 초보자입니다 다름이아니라
mvc 패턴을 공부하고있는 중인데요 모르는게 있어서 이렇게 질문드립니다 ㅠㅠ
public function executeAction($controller) { list($controller, $method) = explode('@', $controller); $controller = "\\App\\Controllers\\$controller"; if(!class_exists($controller)) { throw new \Exception("NO CONTROLLER"); } $controller = new $controller(); if(!method_exists($controller, $method)) { throw new \Exception("NO METHOD"); } return $controller->$method(); }
이런식으로 test.app/home 에접속하면 , test.app/register 에접속하면
homeController→home() 이 실행되고
usersController→register() 이 실행됩니다
여기서 문제는 각 컨트롤러마다 필요한 __construct 이 다릅니다
class UsersController { public $registration; public function __construct(RegisterUser $registration) { $this->registration = $registration; } }
/register 접속시에는 RegisteUser dependency injection 이 필요로하고
다른 컨트롤러는 다른 의존성이 필요로하는데 이럴땐 어떻게해야하나요?
초보이다 보니 질문하는것도 어렵네요 이해가 안가실듯합니다 ㅠㅠ
return $controller->$method();
즉 여기 컨트롤러→매소드가 실행될때마다 그 컨트롤러가 필요로하는 생성자들이 각 컨트롤마다 다른데 이럴땐 어찌해야될런지요 ㅠㅠ
글이 너무 허접한점 죄송합니다 ㅠㅠ
executeAction($controller) 함수를 누가 호출하는 지 모르겠습니다만. 컨트롤러 객체에 주입할 의존 객체를, executeAction() 함수의 두번째 파라미터 받고, new $controller()할 때 넣어 주면 될 것 같네요.
Controller Dispatch, Dynamic Dispatch, PHP Dependency Injection Container 등을 검색해 보시면 좋겠습니다.
php 배우고있는 초보자입니다 다름이아니라
mvc 패턴을 공부하고있는 중인데요 모르는게 있어서 이렇게 질문드립니다 ㅠㅠ
이런식으로 test.app/home 에접속하면 , test.app/register 에접속하면
homeController→home() 이 실행되고
usersController→register() 이 실행됩니다
여기서 문제는 각 컨트롤러마다 필요한 __construct 이 다릅니다
/register 접속시에는 RegisteUser dependency injection 이 필요로하고
다른 컨트롤러는 다른 의존성이 필요로하는데 이럴땐 어떻게해야하나요?
초보이다 보니 질문하는것도 어렵네요 이해가 안가실듯합니다 ㅠㅠ
즉 여기 컨트롤러→매소드가 실행될때마다 그 컨트롤러가 필요로하는 생성자들이 각 컨트롤마다 다른데 이럴땐 어찌해야될런지요 ㅠㅠ
글이 너무 허접한점 죄송합니다 ㅠㅠ