在symfony上使用注释路径在twig上设置href路径

时间:2016-07-04 17:51:01

标签: php symfony twig

我正在使用注释路由,我必须设置一个href路径但是我收到了这条消息

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. 

这是第20行的menu.html.twig

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

这是控制器

<?php

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->setUsername("usuario2");
        $user->setFirstName("aaa");
        $user->setLastName("bb");
        $user->setEmail("bbb@mail.com");
        $user->setPassword("123456");
        $user->setRole('ROLE_ADMIN');
        $user->setIsActive(true);
        $user->setCreatedAt(new DateTime());
        $user->setUpdatedAt(new DateTime());
        $em = $this->getDoctrine()->getManager();
        $em->persist($user);
        $em->flush();*/
        return $this->render('UserBundle:Default:index.html.twig');
        //return new Response(phpinfo());
    }
    /**
    *@Route("/ver")
    */
    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 ));
    }

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

和routing.yml

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

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

我必须激活一些路线或我必须做什么?

1 个答案:

答案 0 :(得分:2)

您需要指定路径的名称才能通过twig生成,因此请按照以下步骤更改控制器注释:

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