【发布时间】:2017-03-01 04:21:02
【问题描述】:
是否可以在 Slim 容器中获取路由占位符的值?我知道我可以通过向请求添加第三个参数来访问占位符,但我希望将其注入,因此我不会在每个请求上分配它。
我已经尝试过 $app->getContainer('router') 但我似乎找不到实际提取占位符值的方法。
例子:
$app = new Slim\App;
$c = $app->getContainer();
$c['Controller'] = function() {
$userId = // how do I get the route placeholder userId?
return new Controller($userId);
};
$app->get('/user/{userId}','Controller:getUserId');
class Controller {
public function __construct($userId) {
$this->userId = $userId;
}
public function getUserId($request,$response) {
return $response->withJson($this->userId);
}
}
【问题讨论】: