[英]Symfony2 how to inject request and get route parameters in controller
My controller method looks following: 我的控制器方法如下:
/**
* @Route("/film/{slugDe}", name="movie_De")
*/
public function movieAction($slugDe)
{
now I need to bind a form with request, but request is not injected. 现在我需要将表单与请求绑定,但是请求不会被注入。 How do I inject request and keep the route parameters? 如何注入请求并保留路由参数?
You could get the request like this from within the controller... 您可以从控制器中获取这样的请求...
$request = $this->get('request_stack')->getCurrentRequest();
Edit: 编辑:
Actually, after thinking about this a bit, I think Martin's answer might be the better route to take. 实际上,经过一番思考之后,我认为马丁的答案可能是更好的选择。 While the above is a perfectly valid way to get the request from the controller, type-hinting for the request is probably the preferred method. 尽管以上是从控制器获取请求的完美有效方法,但请求的类型提示可能是首选方法。 And as shown, you can still type-hint for the request in your controllers action method when you're using slugs. 并且如图所示,当您使用块时,您仍然可以在控制器的操作方法中键入请求的提示。
What is the best way to get the 'Request' object in the controller? 在控制器中获取“请求”对象的最佳方法是什么?
http://symfony.com/blog/new-in-symfony-2-4-the-request-stack http://symfony.com/blog/new-in-symfony-2-4-the-request-stack
You can simply inject request like this: 您可以像这样简单地注入请求:
use Symfony\Component\HttpFoundation\Request;
public function movieAction($slugDe, Request $request)
{
// ...
$form->handleRequest($request);
// ...
}
See http://symfony.com/doc/current/book/controller.html#the-request-as-a-controller-argument 参见http://symfony.com/doc/current/book/controller.html#the-request-as-a-controller-argument
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.