Symfony2 教程弃用函数

Symfony2 tutorial deprecated function

我正在学习 Symfony2 Framework,开始时我找到了这个教程:Tutorial。在我遇到这个问题之前一切都很顺利:

Tutorial Part2 中。在控制器部分创建表单中,我发现 getRequest() 函数已被弃用,并且 bindRequest() 在 class 中找不到。

这两个阻碍了我的教程和学习进度。我有任何其他方法可以在不使用这些函数的情况下构建这个控制器,或者是否有其他函数可以做完全相同的事情。

请参阅 Symfony 文档的 part。它表明你应该使用 handleRequest,像这样:

// Top of page:
use Symfony\Component\HttpFoundation\Request;
...

// controller action
public function newAction(Request $request)
{
    $form = $this->createFormBuilder()
        // ...
        ->getForm();

    $form->handleRequest($request);

    if ($form->isValid()) {
        // perform some action...

        return $this->redirect($this->generateUrl('task_success'));
    }

    return $this->render('AcmeTaskBundle:Default:new.html.twig', array(
        'form' => $form->createView(),
    ));
}

您可能还会发现此 link 有用:Handling Form Submissions

作为控制器参数的请求

以这种方式获取请求对象一开始可能有点混乱,引用文档:

What if you need to read query parameters, grab a request header or get access to an uploaded file? All of that information is stored in Symfony's Request object. To get it in your controller, just add it as an argument and type-hint it with the Request class:

use Symfony\Component\HttpFoundation\Request;

public function indexAction($firstName, $lastName, Request $request)
{
    $page = $request->query->get('page', 1);

    // ...
}

此信息可在 Controller Documentation 上找到。