【问题标题】:Symfony : Route overrides other templatesSymfony:路由覆盖其他模板
【发布时间】:2016-07-16 21:47:17
【问题描述】:

问题在于路由@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(),
    ));
}

Twig 模板,city.html.twig

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

【问题讨论】:

    标签: php annotations symfony symfony-routing


    【解决方案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} 参数的值必须是数字(即数字)。

    之后,像/editcity_123 这样的 URL 将永远不会匹配 /{cityId} 路由,而只会匹配 /editcity_{id} 路由。

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

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

    希望能有所帮助。

    【讨论】:

      猜你喜欢
      • 2017-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-01
      • 2013-02-17
      相关资源
      最近更新 更多