很容易这里是我在最近的项目上采取的步骤。
首先让我们说你有一个HomeActionController
class HomeActionController {
//The below line I have moved into an abstract Controller class
public $view = null;
//This is using Slim Views PhpRenderer
//This allows for a controller to render views can be whatever you need
//I did not like the idea of passing the whole DC it seemed overkill
//The below method I have moved into an abstract Controller class
public function __construct(\Slim\Views\PhpRenderer $view = null){
if($view != null){
$this->view = $view;
}
}
//View could be any action method you want to call it.
public function view(Request $request, Response $response, array $args){
$data['user'] = "John Doe";
return $this->view->render($response, 'templates/home.php', $data);
}
}
现在,你需要能够从一个路由调用这个控制器的一个实例,所以你需要你有控制器加入到DC
你在哪里都被创建的苗条例如,你将需要获得DC,并添加您的控制器的一个实例:
$app = new \Slim\App($config['slim']);
// Get Dependency Container for Slim
$container = $app->getContainer();
$container['HomeActionController'] = new Controller\HomeActionController($container['view']); //Notice passing the view
作为一个说明上述实例s可能是封闭的,但我当时没有看到这一点或做出决定。此外,还有一些延迟加载的方法,我还没有探究,请参阅here了解更多信息。
现在,您需要做的最后一件事就是能够在路线上调用这些不是很大的挑战。
$app->get('/home', 'HomeActionController:view');
就算你不能有参数的作用,但我没有问题只是路过他们一起在请求中,然后从那里得到他们。
你可能会检查其中一个Slim骨骼的灵感。 –