슬림 프레임 워크 기반 API에서 작업 중입니다. mvc 패턴에 따라, 로거와 렌더러를 모든 컨트롤러에 주입하여 내 경로를 컨트롤러 구동으로하고 싶습니다. 시작점으로서 슬림 프레임 워크 3에서 경로 컨트롤러를 구성하는 방법 (mvc 패턴)
내가 예 MVC 슬림형 골격의 수를 조사하고, 하나 개의 특정 가이드 샘플 프로젝트 ( http://jgrundner.com/slim-oo-004-controller-classes/)이 설정에서내 structor 기초로 결정 주입 라우터를 추가함으로써 행해진 다
$container = $app->getContainer();
$container['\App\Controllers\DefaultController'] = function($c){
return new \App\Controllers\DefaultController(
$c->get('logger'),
$c->get('renderer')
);
};
이것은 다음에 좋은 깨끗한 경로와 컨트롤러를 허용 :
경로 예 :
과 같은 응용 프로그램 컨테이너 컨트롤러 16,$app->get('/[{name}]', '\App\Controllers\DefaultController:index');
컨트롤러 예컨대 : 기본 컨트롤러를 확장
namespace App\Controllers;
use Psr\Log\LoggerInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
class DefaultController{
private $logger;
private $renderer;
public function __construct(LoggerInterface $logger, $renderer){
$this->logger = $logger;
$this->renderer = $renderer;
}
public function index(RequestInterface $request, ResponseInterface $response, $args){
// Log message
$this->logger->info("Slim-Skeleton '/' route");
// Render index view
return $this->renderer->render($response, 'index.phtml', $args);
}
public function throwException(RequestInterface $request, ResponseInterface $response, array $args){
$this->logger->info("Slim-Skeleton '/throw' route");
throw new \Exception('testing errors 1.2.3..');
}
}
은 깔끔한 컨트롤러를 유지하지만 당신이 수업을 많이 가지고 때 비효율적이고 지저분한 것 같다 먼저 응용 프로그램 컨테이너마다 새로운 컨트롤러 객체를 추가해야합니다.
더 좋은 방법이 있습니까?
이 문서 http://glenneggleton.com/page/slim-에서 할 그 수있는 더 좋은 방법이있다 3-controllers-and-actions –