1

问题在于 route 的奇怪行为@Route("/{cityId}")。当此路由高于其他路由(例如@Route("/editcity_{id}"))时,它会覆盖模板。

看截图:

    1. 路线/{cityId}是第一个,/editcity_{id}也是最后一个。

当我尝试转到/editcity_2路由/2处理请求并city.html.twig显示此错误时:

  • 2.当/editcity_{id}是第一条路线和/{cityId}第二条路线时,一切正常。

这是我所拥有的:

CityController.php

   <?php
   namespace AppBundle\Controller;
   use AppBundle\Entity\City;
   use AppBundle\Form\Type\CityType;
   use Symfony\Bundle\FrameworkBundle\Controller\Controller;
   use Symfony\Component\HttpFoundation\Request;
   use Symfony\Component\HttpFoundation\Response;
   use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
   class CityController extends Controller
   {
   /**
    * @Route("/addcity")
    * @param Request $request
    * @return Response
    */
   public function addCityAction(Request $request) {
    $city = new City();
    $form = $this->createForm(CityType::class, $city);
    $form->handleRequest($request);
    if ($form->isSubmitted() && $form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($city);
        $em->flush();
        return new Response('Created city id '.$city->getId());
    }
    return $this->render(':appviews:addCity.html.twig', array(
        'form' => $form->createView(),
    ));
}
/**
 * @Route("/cities")
 */
public function citiesAction(){
    $em = $this->getDoctrine()->getEntityManager();
    $cities = $em->getRepository('AppBundle:City')->findAll();
    return $this->render('appviews/citiesList.html.twig',array('cities'=>$cities));
}

/**
 * @Route("/{cityId}")
 */
public function cityAction($cityId) {
    $em = $this->getDoctrine()->getManager();
    $city = $em->getRepository('AppBundle:City')->find($cityId);
    return $this->render('appviews/city.html.twig',array(
        'city' => $city));
}

/**
 * @Route("/editcity_{id}")
 * @return Response
 */
public function editCityAction($id, Request $request) {
    $em = $this->getDoctrine()->getEntityManager();
    $city = $em->getRepository('AppBundle:City')->find($id);
    $form = $this->createForm(CityType::class, $city);
    $form->handleRequest($request);
    if ($form->isSubmitted() && $form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($city);
        $em->flush();
        return new Response('Created city id '.$city->getId());
    }

    return $this->render(':appviews:editCity.html.twig', array(
        'form' => $form->createView(),
    ));
}

树枝模板,city.html.twig

{% extends "appviews/base.html.twig" %}
{% block title %}Cтраница города {% endblock %}
{% block body %}
{{ dump(city)}}
{{ city.link }}
{% endblock %}
4

1 回答 1

1

你得到的是一种正常的行为,因为

Symfony 路由器总是会选择它找到的第一个匹配的路由

并且您的/{cityId}路线等效/*于这些 URL 的真实路线,例如:

  • /123
  • /add-new-article
  • /edit-123
  • /article-123.html
  • /123-article.html

因此,您的/editcity_{id}路线将永远不会匹配,因为这是您的路线/{cityId}。但是您可以通过添加这样的路由要求或路由条件来避免这种情况:

/**
 * @Route("/{cityId}", requirements={ "cityId": "\d+" })
 */

\d+要求是一个正则表达式,表示参数的值必须{cityId}是数字(即数字)。

之后,类似的 URL/editcity_123将永远不会匹配/{cityId}路由,而只会匹配/editcity_{id}一个。

所以不要忘记向你的路线添加要求以获得清晰的代码并避免此类问题,当然当你需要的时候。

参考。: http ://symfony.com/doc/current/book/routing.html#adding-requirements

希望能有所帮助。

于 2016-03-29T11:59:35.413 回答