【问题标题】:No Route Found in Symfony 2.2.1 after Generation of CRUD生成 CRUD 后 Symfony 2.2.1 中找不到路由
【发布时间】:2016-06-02 12:39:07
【问题描述】:

地狱朋友。我正在做一个 symfony 2 项目,我必须在其中添加一个 crud 操作。文件夹结构为 src->M2Catalyst->M2MatchCut and Utilities。在 M2MatchCut 文件夹下,我有一个 StoreBundle。我正在尝试生成一个新的crud,如下所示:

php app/console generate:doctrine:crud
M2CatalystM2MatchCutStoreBundle:Partner
"write" actions [no]? y
Configuration format (yml, xml, php, or annotation) [annotation]:annotation
Routes prefix [/partner]:null
Do you confirm generation [yes]?y

我的routing.yml如下

   m2_catalyst_m2_match_cut_dubit:
    resource: "@M2CatalystM2MatchCutDubitBundle/Controller/"
    prefix:   /

m2_catalyst_m2_match_cut_store:
    resource: "@M2CatalystM2MatchCutStoreBundle/Controller/"
    type: annotation
    prefix:   /product


m2_catalyst_m2_match_cut_match_cut:
    resource: "@M2CatalystM2MatchCutMatchCutBundle/Controller"
    prefix:   /matchcut
    type:     annotation

m2_catalyst_m2_match_cut_api:
    resource: "@M2CatalystM2MatchCutApiBundle/Controller"
    prefix:   /api
    type:     annotation

homepage:
    pattern:  /
    defaults: { _controller: FOSUserBundle:Security:login }

m2_catalyst_m2_match_cut_youtube_api:
    resource: "@M2CatalystM2MatchCutYoutubeApiBundle/Controller"
    prefix:   /youtube
    type:     annotation


fos_user_security:
    resource: "@FOSUserBundle/Resources/config/routing/security.xml"

# Only login and basic security at first

fos_user_profile:
    resource: "@FOSUserBundle/Resources/config/routing/profile.xml"
    prefix: /profile

fos_user_register:
    resource: "@FOSUserBundle/Resources/config/routing/registration.xml"
    prefix: /register

fos_user_resetting:
    resource: "@FOSUserBundle/Resources/config/routing/resetting.xml"
    prefix: /resetting

fos_user_change_password:
    resource: "@FOSUserBundle/Resources/config/routing/change_password.xml"
    prefix: /profile

m2_catalyst_m2_match_cut_user:
    resource: "@M2CatalystM2MatchCutUserBundle/Resources/config/routing.yml"
    prefix:   /

m2_catalyst_m2_match_cut_home:
    resource: "@M2CatalystM2MatchCutHomeBundle/Resources/config/routing.yml"
    prefix:   /


sonata_page_cache:
    resource: '@SonataCacheBundle/Resources/config/routing/cache.xml'
    prefix: /

# app/config/routing.yml
admin:
    resource: '@SonataAdminBundle/Resources/config/routing/sonata_admin.xml'
    prefix: /

_sonata_admin:
    resource: .
    type: sonata_admin
    prefix: /

gallery:
    resource: '@SonataMediaBundle/Resources/config/routing/gallery.xml'
    prefix: /media/gallery

media:
    resource: '@SonataMediaBundle/Resources/config/routing/media.xml'
    prefix: /media

但是当我尝试访问 M2Catalyst/web/app_dev.php/m2matchcut/store/movie 时,它​​显示“没有找到“GET /m2matchcut/store/movie”的路由

出了什么问题,谁能帮忙?

我的Controller类PartnerController如下:

<?php

namespace M2Catalyst\M2MatchCut\StoreBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use M2Catalyst\M2MatchCut\StoreBundle\Entity\Partner;
use M2Catalyst\M2MatchCut\StoreBundle\Form\PartnerType;

/**
 * Partner controller.
 *
 * @Route("/partner")
 */
class PartnerController extends Controller
{

    /**
     * Lists all Partner entities.
     *
     * @Route("/", name="partner")
     * @Method("GET")
     * @Template()
     */
    public function indexAction()
    {
        $em = $this->getDoctrine()->getManager();

        $entities = $em->getRepository('M2CatalystM2MatchCutStoreBundle:Partner')->findAll();

        return array(
            'entities' => $entities,
        );
    }
    /**
     * Creates a new Partner entity.
     *
     * @Route("/", name="partner_create")
     * @Method("POST")
     * @Template("M2CatalystM2MatchCutStoreBundle:Partner:new.html.twig")
     */
    public function createAction(Request $request)
    {
        $entity  = new Partner();
        $form = $this->createForm(new PartnerType(), $entity);
        $form->bind($request);

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

            return $this->redirect($this->generateUrl('partner_show', array('id' => $entity->getId())));
        }

        return array(
            'entity' => $entity,
            'form'   => $form->createView(),
        );
    }

    /**
     * Displays a form to create a new Partner entity.
     *
     * @Route("/new", name="partner_new")
     * @Method("GET")
     * @Template()
     */
    public function newAction()
    {
        $entity = new Partner();
        $form   = $this->createForm(new PartnerType(), $entity);

        return array(
            'entity' => $entity,
            'form'   => $form->createView(),
        );
    }

    /**
     * Finds and displays a Partner entity.
     *
     * @Route("/{id}", name="partner_show")
     * @Method("GET")
     * @Template()
     */
    public function showAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('M2CatalystM2MatchCutStoreBundle:Partner')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Partner entity.');
        }

        $deleteForm = $this->createDeleteForm($id);

        return array(
            'entity'      => $entity,
            'delete_form' => $deleteForm->createView(),
        );
    }

    /**
     * Displays a form to edit an existing Partner entity.
     *
     * @Route("/{id}/edit", name="partner_edit")
     * @Method("GET")
     * @Template()
     */
    public function editAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('M2CatalystM2MatchCutStoreBundle:Partner')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Partner entity.');
        }

        $editForm = $this->createForm(new PartnerType(), $entity);
        $deleteForm = $this->createDeleteForm($id);

        return array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        );
    }

    /**
     * Edits an existing Partner entity.
     *
     * @Route("/{id}", name="partner_update")
     * @Method("PUT")
     * @Template("M2CatalystM2MatchCutStoreBundle:Partner:edit.html.twig")
     */
    public function updateAction(Request $request, $id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('M2CatalystM2MatchCutStoreBundle:Partner')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Partner entity.');
        }

        $deleteForm = $this->createDeleteForm($id);
        $editForm = $this->createForm(new PartnerType(), $entity);
        $editForm->bind($request);

        if ($editForm->isValid()) {
            $em->persist($entity);
            $em->flush();

            return $this->redirect($this->generateUrl('partner_edit', array('id' => $id)));
        }

        return array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        );
    }
    /**
     * Deletes a Partner entity.
     *
     * @Route("/{id}", name="partner_delete")
     * @Method("DELETE")
     */
    public function deleteAction(Request $request, $id)
    {
        $form = $this->createDeleteForm($id);
        $form->bind($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $entity = $em->getRepository('M2CatalystM2MatchCutStoreBundle:Partner')->find($id);

            if (!$entity) {
                throw $this->createNotFoundException('Unable to find Partner entity.');
            }

            $em->remove($entity);
            $em->flush();
        }

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

    /**
     * Creates a form to delete a Partner entity by id.
     *
     * @param mixed $id The entity id
     *
     * @return \Symfony\Component\Form\Form The form
     */
    private function createDeleteForm($id)
    {
        return $this->createFormBuilder(array('id' => $id))
            ->add('id', 'hidden')
            ->getForm()
        ;
    }
}

【问题讨论】:

  • 显示你的控制器类
  • 更新了上面的问题@Andrzej

标签: symfony routes


【解决方案1】:

你的路错了。

如果你想从你的控制器渲染 indexAction,这个路径应该适用于那个路由配置:

.../app_dev.php/movie/partner

【讨论】:

  • 实际上我尝试制作另一个名为 Movie 的 crud。并尝试在路由中使用前缀 / 来定义它。在 /movie 中改变了它。我现在从 routing.yml m2_catalyst_m2_match_cut_store_movie: resource: "@M2CatalystM2MatchCutStoreBundle/Controller/" type: annotation prefix: /movie 中删除这部分代码
猜你喜欢
  • 2017-12-29
  • 2019-02-11
  • 2016-03-07
  • 2016-07-03
  • 2018-05-30
  • 1970-01-01
  • 1970-01-01
  • 2017-01-01
相关资源
最近更新 更多