問題出在路由@Route("/{cityId}")
的奇怪行爲。當此路由比其他路由更高時(例如@Route("/editcity_{id}")
),它將覆蓋模板。Symfony:Route覆蓋其他模板
請看截圖:
-
- 路線
/{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 %}
非常感謝!問題解決了! –