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

2024-03-31

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


这是我使用的方法:

创建查找表单的操作:

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_分页器 https://github.com/KnpLabs/KnpPaginatorBundle/blob/master/Resources/doc/templates.md

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

管理归档表单和结果页面的最佳方式 的相关文章

随机推荐