简体   繁体   中英

Symfony2 annotation Unable to generate a URL for the named route

As far as I've searched similar questions I did not found one that solves my problem.

This is the entry in app/config/routing.yml:

basym_address:
    resource: "@BasymAddressBundle/Controller/"
    type:     annotation
    prefix:   /

This is the corresponding controller:

namespace Basym\AddressBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

/**
 * Class DefaultController
 *
 * @package Basym\AddressBundle\Controller
 *
 * @Route("/address")
 */
class DefaultController extends Controller
{
    /**
     * @Route("/{name}", defaults={"name" = "Christian Lauer"})
     * @Method({"GET", "POST"})
     * @Template()
     */
    public function indexAction($name = "Christian Lauer")
    {
        return array('name');
    }
}

And here is the code from which the exception is thrown (from a MenuBuilder):

        $menu->addChild('Home', array('route' => 'address'));

And this is the output from app/console router:match /address:

> php.exe app\console router:match /address
Route "basym_address_default_index" matches

[router] Route "basym_address_default_index"
Name         basym_address_default_index
Path         /address/{name}
Host         ANY
Scheme       ANY
Method       GET|POST
Class        Symfony\Component\Routing\Route
Defaults     _controller: BasymAddressBundle:Default:index
             name: Christian Lauer
Requirements 
Options      compiler_class: Symfony\Component\Routing\RouteCompiler
Path-Regex   #^/address(?:/(?P<name>[^/]++))?$#s

The twig portion which calls the MenuBuilder looks as this (line: 19:

{{ knp_menu_render('BasymSystemBundle:MenuBuilder:mainMenu') }}

I get "An exception has been thrown during the rendering of a template ("Unable to generate a URL for the named route "address" as such route does not exist.") in kernel.root_dir/Resources/views/layout.html.twig at line 19". Why?

Any help appreciated.

only routes that point to a function can be used. You have only one working route. It's /address/{name}

/address only references to the controller class DefaultController

The Following code would support the route /address:

namespace Basym\AddressBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

/**
 * Class DefaultController
 *
 * @package Basym\AddressBundle\Controller
 *
 * @Route("/address")
 */
class DefaultController extends Controller
{
    /**
     * @Route("/{name}", defaults={"name" = "Christian Lauer"})
     * @Method({"GET", "POST"})
     * @Template()
     */
    public function indexAction($name = "Christian Lauer")
    {
        return array('name');
    }

    /**
     * @Route("/")
     * @Method({"GET", "POST"})
     */
    public function anotherAction()
    {
        return new Response();
    }
}

我认为您应该使用basym_address_default_index作为路由名称,而不是address

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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