user2794692
user2794692

Reputation: 391

Unable to generate route

I have a helper to get in the view what is my current route, but this line always return me a "NULL":

I check with console route:debug and all routes are right. And my application works fine.

$currentRoute = $request->attributes->get('_route');

My helper works with other functions and I have Container injected.

Any idea? Thanks.

Upvotes: 0

Views: 62

Answers (1)

Marcin Nabiałek
Marcin Nabiałek

Reputation: 111829

I hope it will help you some way because I'm not Symfony2 expert - I'm showing you how it works in my case.

In my Symfony2 projects I have routes defined this way:

routing.yml

mnab_contact:
    resource: "@MnabContactBundle/Resources/config/routing.yml"
    prefix:   /

mnab_projects:
    resource: "@MnabProjectsBundle/Resources/config/routing.yml"
    prefix:   /

mnab_article:
    resource: "@MnabArticleBundle/Resources/config/routing.yml"
    prefix:   /

and for example for my ArticleBundle my routing.yml file is:

default:
    pattern:  /{anything}
    defaults: { _controller: MnabArticleBundle:Default:index , anything: null}
    requirements:
      anything: .*

so as you see I also use parameters.

For my projects I have routing.yml :

projects:
    pattern:  /projects/{anything}
    defaults: { _controller: MnabProjectsBundle:Default:index, anything: ''}
    requirements:
      anything: .*

And in both controllers I have code like that:

<?php

namespace Mnab\ArticleBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use Symfony\Component\HttpFoundation\Request;

use \Symfony\Component\HttpFoundation\Response;
use \Symfony\Component\HttpFoundation\JsonResponse;


use \Mnab\Symfony\BaseController;

use \Mnab\DB\DbInterface;


class DefaultController extends BaseController
{
    public function indexAction(Request $request, $anything)
    {

        echo $request->attributes->get('_route');
        die();

    }
}

for both controller I have displayed either projects or default depending on url. Route names are being taken from routing.yml files from each Bundle directory.

Upvotes: 1

Related Questions