symfony - 管理归档表单和结果页面的最佳方式

标签 symfony

在 Symfony 中管理过滤器页面和结果页面的最佳方式是什么? 我有一个管理过滤器表单并执行查询的 Controller 。该查询的结果必须传入另一个 Controller 操作。结果必须显示在另一个 Controller 操作中,因为我使用了 knp_paginator。如果我在过滤器表单的相同 Controller 操作中呈现结果,则当更改页面时, Controller 显示过滤器表单而不是结果。

最佳答案

这是我使用的方法:

创建查找表单的操作:

public function findAction(Request $request)
{

    $form = $this->createFindForm($request);

    $form->handleRequest($request);

    if(($form->isSubmitted() && !$request->isXmlHttpRequest()))
    {
        if(($this->isValidFindForm($form) && $form->isValid()))
        {
            $parm = $request->request->get('findForm');
            return $this->redirect($this->generateUrl('list_documents',$parm));
        }
    }

    return $this->render(
            'myBundle:Documents:document\document_find.html.twig',
            array('form' => $form->createView())
    );
}

private function createFindForm(Request $request)
{
    $form = $this->createForm(
                new findDocumentType(
                    $this->getDoctrine()->getManager(),
                    $request
                ),
                null,
                array(
                    'action' => $this->generateUrl('find_documents'),
                    'method' => 'POST',
                )
            );
    return $form;
}

我使用了 $parm = $request->request->get('findForm');获取查询字符串。 “findForm”是我的过滤表单的名称。

重定向操作:

public function listAction(Request $request)
{
    $documents = $this->searchDocument($request);
    $paginator = $this->get('knp_paginator');
    $pagination = $paginator->paginate(
            $documents,
            $this->get('request')->query->get('page', 1)
    );
    return $this->render(
            'myBundle:Documents:document\document_list.html.twig',
            array('pagination' => $pagination)
    );
}

请求被传递到搜索函数(请求包含查询字符串) 在搜索操作中:

private function searchDocument(Request $request)
{
    $parm = $request->query;
    $repository = $this->getDoctrine()->getRepository('docliteBundle:Document\\document');
    $query = $repository
                ->createQueryBuilder('d');
 ....

通过 $request->query->get() 我可以访问所有参数。

希望这对某人有帮助。

附注对于我使用的分页 KNP_paginator

关于symfony - 管理归档表单和结果页面的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28339155/

相关文章:

php - 交响乐 2 : changing user in database does not cause logout

php - 当我尝试在 symfony2 中获取管理页面时出现此错误

symfony - 停留在prePersist功能时如何取消保存实体

php - 如何解析 symfony2 中非 Controller 类中的路径

php - 包括当前目录中的 Twig 文件

mysql - Symfony 在 MySQL 数据库中设置日期时间

php - Symfony2 安全,使用 sha512,不工作。收到错误的凭据消息

php - SonataUser - 从管理表单中删除字段

php - 安装错误: Overwrite Akeneo ProductController

php - 用其他内容替换 SELECT 语句