Проблема в странном поведении маршрута @Route("/{cityId}")
. Когда этот маршрут является верхним, чем другие маршруты (например, @Route("/editcity_{id}")
), он переопределяет шаблон.Symfony: Route переопределяет другие шаблоны
Посмотрите на скриншоты:
-
- маршрут
/{cityId}
является первым и/editcity_{id}
является последним.
- маршрут
Когда я пытаюсь перейти к /editcity_2
маршруту /2
обрабатывает запрос и city.html.twig
показать эту ошибку:
- 2.when
/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(),
));
}
Twig шаблон, city.html.twig
{% extends "appviews/base.html.twig" %}
{% block title %}Cтраница города {% endblock %}
{% block body %}
{{ dump(city)}}
{{ city.link }}
{% endblock %}
Большое спасибо! Задача решена! –