无法在 Symfony2 中路由随机页面

Unable to route random page in Symfony2

我刚开始使用 symfony2 手似乎无法完成第一个示例,创建一个带有随机数的页面。

我尝试在此处遵循 symfony2 的演练:Creating pages

Acme DemoBundle 已经安装,所以我的 config_dev 已经加载了 Acme DemoBundle routing.yml,如下所示:

random:
  path:     /random/{limit}
  defaults: { _controller: AcmeDemoBundle:Random:index }

(在其他工作正常的条目中)

我的 RandomController 看起来像这样:

<?php
/**
 * Created by PhpStorm.
 * User: Richard
 * Date: 23.04.2015
 * Time: 22:46
 */

namespace Acme\DemoBundle\Controller;

use Symfony\Component\HttpFoundation\Response;

class RandomController {

    public function indexAction($limit)
    {
        return new Response(
            '<html><body>Number: ' . rand(1,$limit) . '.</body></html>'
        );
    }
}

奇怪的是,当用debug-tool检查时,它告诉我它可以找到路线:

_random /random/{limit} Route matches!

但不是来自 header:

Routing for "/random/10"

Route:  No matching route
Route parameters:  No parameters
Route matching logs

此外,即使限制应该是可选的,如果省略它也不会“匹配”,这也适用于使用可选参数的其他路由。

有什么想法吗?

编辑

为 app/console 添加输出 router:debug:

vagrant@scotchbox:/var/www$ php app/console router:debug
[router] Current routes
Name                      Method Scheme Host Path                              
_wdt                      ANY    ANY    ANY  /_wdt/{token}                     
_profiler_home            ANY    ANY    ANY  /_profiler/                       
_profiler_search          ANY    ANY    ANY  /_profiler/search                 
_profiler_search_bar      ANY    ANY    ANY  /_profiler/search_bar             
_profiler_purge           ANY    ANY    ANY  /_profiler/purge                  
_profiler_info            ANY    ANY    ANY  /_profiler/info/{about}           
_profiler_phpinfo         ANY    ANY    ANY  /_profiler/phpinfo                
_profiler_search_results  ANY    ANY    ANY  /_profiler/{token}/search/results 
_profiler                 ANY    ANY    ANY  /_profiler/{token}                
_profiler_router          ANY    ANY    ANY  /_profiler/{token}/router         
_profiler_exception       ANY    ANY    ANY  /_profiler/{token}/exception      
_profiler_exception_css   ANY    ANY    ANY  /_profiler/{token}/exception.css  
_configurator_home        ANY    ANY    ANY  /_configurator/                   
_configurator_step        ANY    ANY    ANY  /_configurator/step/{index}       
_configurator_final       ANY    ANY    ANY  /_configurator/final              
_twig_error_test          ANY    ANY    ANY  /_error/{code}.{_format}          
homepage                  ANY    ANY    ANY  /app/example                      
_welcome                  ANY    ANY    ANY  /                                 
_demo_login               ANY    ANY    ANY  /demo/secured/login               
_demo_security_check      ANY    ANY    ANY  /demo/secured/login_check         
_demo_logout              ANY    ANY    ANY  /demo/secured/logout              
acme_demo_secured_hello   ANY    ANY    ANY  /demo/secured/hello               
_demo_secured_hello       ANY    ANY    ANY  /demo/secured/hello/{name}        
_demo_secured_hello_admin ANY    ANY    ANY  /demo/secured/hello/admin/{name}  
_demo                     ANY    ANY    ANY  /demo/                            
_demo_hello               ANY    ANY    ANY  /demo/hello/{name}                
_demo_contact             ANY    ANY    ANY  /demo/contact                     
random                    ANY    ANY    ANY  /random/{limit}  

问题是您使用的是 /web/app.php 前端控制器。这会加载 prod 环境,而 AcmeDemoBundle 仅在 dev 环境中加载(正如您在 AppKernel#registerBundles() 中看到的 app/AppKernel.php).更重要的是,路由存储在routing_dev.yml中,它也只在开发环境中加载。

简而言之:路由未在生产环境中加载。

解决方案:创建一个 AppBundle 并在所有环境中启用它,并在 app/config/routing.yml 文件中注册它的路由。

更好的是:不要将生产环境用作开发环境。这会导致很多问题,包括每次更改内容时都需要清除缓存;没有开发设置;无法使用像 Web Dev Toolbar 这样的好工具;有一个奇怪的部署过程,因为您不希望在生产中启用调试;还有更多。