[英]PHP Silex - Service Controller - passing a parameter
According to : 根据 :
Silex - Service Controller Doc Silex-服务控制器文档
I can define a route like this (after a couple of extra code of corse): 我可以定义一条这样的路线(经过几条额外的corse代码):
$app->get('/posts.json', "posts.controller:indexJsonAction");
$ app-> get('/ posts.json',“ posts.controller:indexJsonAction”);
But ... how can I pass the url used to the indexJsonAction function? 但是...如何将使用的网址传递给indexJsonAction函数?
You should be mapping that directly to the route, such as: 您应该将其直接映射到路由,例如:
$app->get('/posts.json/{param1}/{param2}, 'posts.controller:indexJsonAction');
This way, in your controller, you can expect those parameters: 这样,在控制器中,您可以期望这些参数:
public function indexJsonAction($param1, $param2) {
//now you have access to these variables.
}
Furthermore, silex uses Symfony's request under the hood, so you could also just inject the Request into the controller and get any input from the Request; 而且,silex在后台使用Symfony的请求,因此您也可以将请求注入控制器,并从请求中获取任何输入。
public function indexJsonAction(Request $request) {
// use $request->get('param1'); etc
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.