【发布时间】: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 环境