Symfony2 404错误:找不到对象(ParamConverter错误)

时间:2017-03-20 12:21:25

标签: php symfony parameters controller http-status-code-404

我创建了一个名为search.html.twig的新边界类,但当我转到URL(http://localhost:8000/shrubs/search)时出现以下错误:

  

错误 - 未捕获的PHP异常Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException:"找不到AppBundle \ Entity \ Shrubs对象。"在C:\ Users \ rosmith \ shrub_search \ vendor \ sensio \ framework-extra-bundle \ Request \ ParamConverter \ DoctrineParamConverter.php第66行

我的ParamConverter一定有问题吗?我的注释是否正确?我阅读了symfony文档,但它对我没有意义。这是我的控制者:

 /**
     * Finds and displays a shrub entity.
     *
     * @Route("/search", name="shrubs_search")
     * @ParamConverter("post", class="AppBundle:Shrubs")
     */
    private function searchAction(Request $request)
    {
        $shrub = new Shrubs();
        $form = $this->createForm('AppBundle\Form\ShrubsType', $shrub)
            ->add('botanicalname', TextType::class, array('label' => 'Botanical Name:'))
            ->add('commonname', TextType::class, array('label' => 'Common Name:'))
            ->add('wetsoil', CheckboxType::class, array('label' => 'Tolerates Wet Soil:'))
            ->add('moistsoil', CheckboxType::class, array('label' => 'Prefers Moist Soil:'))
            ->add('peatysoil', CheckboxType::class, array('label' => 'Prefers Peaty Soil:'))
            ->add('welldrainedsoil', CheckboxType::class, array('label' => 'Prefers Well-drained Soil:'))
            ->add('drought', CheckboxType::class, array('label' => 'Tolerates Drought:'))
            ->add('claysoil', CheckboxType::class, array('label' => 'Tolerates Clay Soil:'))
            ->add('sandysoil', CheckboxType::class, array('label' => 'Prefers Sandy Soil:'))
            ->add('loamsoil', CheckboxType::class, array('label' => 'Prefers Loam Soil:'))
            ->add('infertilesoil', CheckboxType::class, array('label' => 'Tolerates Infertile Soil:'))
            ->add('richsoil', CheckboxType::class, array('label' => 'Prefers Rich Soil:'))
            ->add('compactedsoil', CheckboxType::class, array('label' => 'Tolerates Compacted Soil:'))
            ->add('cityconditions', CheckboxType::class, array('label' => 'Tolerates City Conditions:'))
            ->add('pollution', CheckboxType::class, array('label' => 'Tollerates Pollution:'))
            ->add('salt', CheckboxType::class, array('label' => 'Tolerates Salt Conditions:'))
            ->add('windy', CheckboxType::class, array('label' => 'Tolerates Windy Conditions:'))
            ->add('shade', CheckboxType::class, array('label' => 'Prefers Shade:'))
            ->add('partshade', CheckboxType::class, array('label' => 'Prefers Part Shade:'))
            ->add('fullsun', CheckboxType::class, array('label' => 'Prefers Full Sun:'))
            ->add('pestproblem', CheckboxType::class, array('label' => 'Pest Problem:'))
            ->add('borderlinehardy', CheckboxType::class, array('label' => 'BorderLine Hardy'));

        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($shrub);
            $em->flush($shrub);

            return $this->redirectToRoute('shrubs_show', array('id' => $shrub->getNumber()));
        }

        return $this->render('shrubs/new.html.twig', array(
            'shrub' => $shrub,
            'form' => $form->createView(),
        ));
    }

1 个答案:

答案 0 :(得分:2)

您的路线应包含post参数。

@Route("/search/{post}", name="shrubs_search")

此外,您的方法应将此参数作为方法参数。