【问题标题】:How to use KnpPaginatorBundle to paginate results for a search form?如何使用 KnpPaginatorBundle 对搜索表单的结果进行分页?
【发布时间】:2018-08-16 11:26:53
【问题描述】:

我正在开发一个 Symfony 2 项目,我使用了 KnpPaginatorBundle,第一页正常工作,但第二页显示此错误:“控制器必须返回响应(给定 null)。您是否忘记添加返回语句在你的控制器的某个地方?“我不明白这个请帮助我


这是我的控制器:

public function searchAction(Request $request) {
    $search_form = $this->createForm(new SearchInterventionBatimentType()); 

    if ($request->isMethod('post')) {
        $search_form->handleRequest($request);
        if ($search_form->isValid()) {
            $data = $search_form->getData();
            $from = $data['from'];
            $to = $data['to'];
            $intervenant= $data['intervenant'];
            $type= $data['type'];
            $batiment = $data['batiment'];

            $em = $this->getDoctrine()->getManager();
            $intervention = new InterventionBatiment();

            if(is_null($intervenant) && is_null($type) && is_null($batiment)) {
                $intervention = $em->getRepository('SecteurBundle:InterventionBatiment')->findByDate($from,$to);
            } elseif(is_null($type) && is_null($batiment)) {
                $intervention = $em->getRepository('SecteurBundle:InterventionBatiment')->findByDateAndIntervenant($from,$to,$intervenant);    
            } elseif (is_null($intervenant) && is_null($batiment)) {
                $intervention = $em->getRepository('SecteurBundle:InterventionBatiment')->findByDateAndType($from,$to,$type);
            } elseif (is_null($intervenant) && is_null($type)) {
                $intervention = $em->getRepository('SecteurBundle:InterventionBatiment')->findByDateAndBatiment($from,$to,$batiment);
            } elseif (is_null($batiment)) {
                $intervention = $em->getRepository('SecteurBundle:InterventionBatiment')->findByDateAndIntervenantAndType($from,$to,$type,$intervenant);
            } else {
                $intervention = $em->getRepository('SecteurBundle:InterventionBatiment')->findByAll($from,$to,$type,$intervenant,$batiment);

            }

            $paginator= $this->get('knp_paginator');
            $result= $paginator->paginate(
                    $intervention, /* query NOT result */
                    $request->query->getInt('page', 1)/*page number*/,
                    $request->query->getInt('limit', 8)/*limit per page*/
            );

            return $this->render(
                    'SecteurBundle:InterventionBatiment:recherche.html.twig',
                    array('interventions' => $result,'form' => $search_form->createView())
            );
        }
    }
}

【问题讨论】:

    标签: symfony-2.8


    【解决方案1】:

    控制器应该总是需要发送响应。

    在您的情况下,如果表单无效

    ,您需要发送回复
    if ($search_form->isValid()) {
    }
    
    return your response here too (if the form is not valid)
    

    【讨论】:

      猜你喜欢
      • 2014-05-13
      • 2021-05-22
      • 1970-01-01
      • 2015-04-27
      • 1970-01-01
      • 2016-03-21
      • 1970-01-01
      • 2013-03-02
      相关资源
      最近更新 更多