简体   繁体   English

无法生成路线

[英]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": 我有一个助手来查看我当前的路线,但是这一行总是返回“ NULL”:

I check with console route:debug and all routes are right. 我检查控制台route:debug,所有路由都正确。 And my application works fine. 而且我的应用程序运行正常。

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

My helper works with other functions and I have Container injected. 我的助手与其他功能一起工作,并且注入了Container。

Any idea? 任何想法? Thanks. 谢谢。

I hope it will help you some way because I'm not Symfony2 expert - I'm showing you how it works in my case. 我希望它能对您有所帮助,因为我不是Symfony2专家-我向您展示了它在我的情况下如何工作。

In my Symfony2 projects I have routes defined this way: 在我的Symfony2项目中,我以这种方式定义了路由:

routing.yml 使用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: 例如,对于我的ArticleBundle,我的routing.yml文件是:

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 : 对于我的项目,我有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. 对于两个控制器,我都显示了projectsdefault具体取决于URL。 Route names are being taken from routing.yml files from each Bundle directory. 路由名称是从每个Bundle目录中的routing.yml文件中获取的。

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

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