问题在于 route 的奇怪行为@Route("/{cityId}")
。当此路由高于其他路由(例如@Route("/editcity_{id}")
)时,它会覆盖模板。
看截图:
- 路线
/{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 %}