2016-03-29 143 views
1

問題出在路由@Route("/{cityId}")的奇怪行爲。當此路由比其他路由更高時(例如@Route("/editcity_{id}")),它將覆蓋模板。Symfony:Route覆蓋其他模板

請看截圖:

    1. 路線/{cityId}是第一和/editcity_{id}是最後一個。

當我嘗試去/editcity_2路線/2手柄請求和city.html.twig顯示此錯誤:

  • 2.當/editcity_{id}是第一和/{cityId}是th第二條路線,一切運作良好。

這裏我有什麼:

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 %} 

回答

1

你得到了什麼是正常的行爲,因爲

的Symfony的路由器總是會選擇第一個匹配的路由發現

和你/{cityId}路線相當於/*這對於這些網址,例如真:

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

所以,因爲它是你的/{cityId}一個的情況下,您/editcity_{id}路線將永遠不會被匹配。但就可以避免通過添加路線要求或路線的條件是這樣的:

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

\d+要求是一個正則表達式,指出{cityId}參數的值必須是一個數字(即,數量)。

,並在此之後,像/editcity_123的網址永遠比不上/{cityId}路線,但只有一個/editcity_{id}

所以,不要忘記給你的路線添加要求,以獲得清晰的代碼,並避免這種問題,當然,當你需要的時候。

Réf。 : http://symfony.com/doc/current/book/routing.html#adding-requirements

希望能有所幫助。

+0

非常感謝!問題解決了! –