无法在Symfony2

时间:2015-04-23 21:32:20

标签: symfony routing

我刚刚开始使用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>'
        );
    }
}

奇怪的是,在使用调试工具进行检查时,它会告诉我它可以找到路径:

_random /random/{limit} Route matches!

但不是来自标题:

Routing for "/random/10"

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

另外,即使认为限制应该是optionall,如果它被省略,它也不会#34;匹配&#34;,这也适用于使用可选参数的其他路由。

有什么想法吗?

最诚挚的问候, 理查德

修改

为app / console路由器添加了输出: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}  

1 个答案:

答案 0 :(得分:1)

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

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

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

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

相关问题