如何在symfony 2中设置自己的索引路由


How to set your own index route in symfony 2?

我的问题是,我不能设置我的控制器作为主页。我一直在搜索一点,我发现这一点,但它不工作,当我在生产环境中运行它。它只能在dev环境下工作。

This is my routing.yml:

homepage:
path: /
defaults: { _controller: DictionaryBundle:Grid:index}
app:
resource: @AppBundle/Controller/
type:     annotation
dictionary:
resource: @AppBundle/DictionaryBundle/Controller/
type:     annotation

fos_user:
resource: "@FOSUserBundle/Resources/config/routing/all.xml"`

这是我的路由routing_dev.yml:

_wdt:
resource: "@WebProfilerBundle/Resources/config/routing/wdt.xml"
prefix:   /_wdt
_profiler:
resource: "@WebProfilerBundle/Resources/config/routing/profiler.xml"
prefix:   /_profiler
 _configurator:
resource: "@SensioDistributionBundle/Resources/config/routing/webconfigurator.xml"
prefix:   /_configurator
 _main:
resource: routing.yml
homepage:
path: /
defaults: { _controller: DictionaryBundle:Grid:index}

这是我的函数:

/**
 * @Route("/{lang}", name = "showTranslationsGrid", defaults={"lang":"-"}, 
 * requirements={"lang":"-|en|fr|pt|it"})
 * @Template("AppBundle:Dictionary:grid.html.twig")
 * Renderiza la vista grid.html.twig que se encarga de generar el grid y cargar los datos
 */
public function indexAction($lang = "-")
{
    //utilizamos el servicio para obtener los belong
    $belongs = $this->forward('dictionary_util:getBelongsAction')->getContent();
    $languages = $this->forward('dictionary_util:getLanguagesAction')->getContent();
    $langs=json_decode($languages,true );
    return array('lang' => $lang, 'belongs'=> $belongs ,'languages'=> $languages );   
}

所以,有人知道为什么有些东西在app/Resources/views/default中渲染index.html.twig,而不是像开发环境那样使用我的控制器和渲染我的模板吗?我也删除了AcmeDemoBundle跟随本教程,我已经清除了几次缓存。

我正在使用symfony 2。我前几天更新到了symfony 2.7。

问题是轮询文件中语句的顺序。它不能在生产环境中工作,因为主页位于文件的顶部,就在它的下面,它指的是AppBundle中的控制器。当我安装symfony时,默认设置了DefaultController(我认为它只有AcmeDemoBundle)。这个控制器重写了之前的主页声明。我不会删除这个问题,只是为了明确顺序很重要,特别是如果你和其他人在同一个项目中工作。