【问题标题】:Unable to route random page in Symfony2无法在 Symfony2 中路由随机页面
【发布时间】:2015-04-23 21:32:20
【问题描述】:

我刚开始使用 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

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

有什么想法吗?

编辑

为应用程序/控制台路由器添加了输出:调试:

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}  

【问题讨论】:

  • 你如何尝试访问路由?并且限制不是可选的,因为它没有指定默认值(如here 解释)
  • 您好,不确定您所说的访问是什么意思,但我正在使用浏览器访问此地址:localhost/random/10
  • 本地主机指向哪里?给/web/app.php/web/app_dev.php
  • Web/app.php 修改为运行 dev 环境而不是 prod 环境
  • Web/app.php 修改为运行 dev 环境而不是 prod 环境

标签: symfony routes


【解决方案1】:

问题是您使用的是/web/app.php 前端控制器。这会加载 prod 环境,而 AcmeDemoBundle 仅加载在 dev 环境中(如您在 AppKernel#registerBundles() 中看到的 app/AppKernel.php 中所示)。更有什者,路由存储在routing_dev.yml中,同样只在dev环境中加载。

简而言之:生产环境中没有加载路由。

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

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

【讨论】:

  • 谢谢 :) 这解决了问题。我想将 $kernel = new AppKernel('prod', false); 更改为 $kernel = new AppKernel('dev', false);不够
猜你喜欢
  • 2020-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-18
  • 2015-08-18
相关资源
最近更新 更多