简体   繁体   English

在symfony上的注释路径上设置树枝上的href路径

[英]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 我正在使用注释路由,必须设置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. 

this is menu.html.twig at line 20 这是第20行的menu.html.twig

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

and this is the controller 这是控制器

<?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");
    }
}

and routing.yml 和routing.yml

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

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

I have to activate some route or what I have to do? 我必须激活某些路线或必须做什么?

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");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM