AFS AFS - 1 year ago 98
PHP Question

Set href path on twig with annotation route on symfony

I'am using annotations routes and I have to set an href path but I get this message

An exception has been thrown during the rendering of a template ("Unable to generate a URL for the named route "crear" as such route does not exist.") in menu.html.twig at line 20.

this is menu.html.twig at line 20

<li><a href="{{ path('crear') }}">Crear</a></li>

and this is the controller


namespace UserBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
use UserBundle\Entity\User;
use \DateTime;

class DefaultController extends Controller
* @Route("/")
public function indexAction()
/*$user = new User();
$user->setCreatedAt(new DateTime());
$user->setUpdatedAt(new DateTime());
$em = $this->getDoctrine()->getManager();
return $this->render('UserBundle:Default:index.html.twig');
//return new Response(phpinfo());
public function viewAction(){
$repository = $this->getDoctrine()->getRepository('UserBundle:User');
$users = $repository->findAll();
//return new Response($user->getEmail());
return $this->render('UserBundle:User:view.html.twig',array('users'=>$users ));

public function createAction(){
return new Response("Crear");

and routing.yml

resource: "@UserBundle/Controller/"
type: annotation
prefix: /

resource: "@AppBundle/Controller/"
type: annotation

I have to activate some route or what I have to do?

Answer Source

You need to specify a name for the route in order to generate via twig, so change the controller annotation as follow:

*@Route("/crear", name="crear")
public function createAction(){
  return new Response("Crear");