Symfony2路由不支持的密钥 [英] Symfony2 routing unsupported keys
问题描述
routing.yml
里面创建了这个路由: acme_demo_homepage:
路径:/ hello / {name}
默认值:{_controller:AcmeDemoBundle:默认值:index}
随机:
路径:/ random / {limit}
defaults:{_controller:AcmeDemoBundle:Random:index}
和Eclipse告诉我在默认值
被声明的行中的错误,并告诉我:
是意外的。
我创建了控制器:
<?php
命名空间Acme \\DemoBundle\Controller;
使用Symfony\Component\HttpFoundation\Response;
class RandomController
{
public function indexAction($ limit)
{
return new Response(' html>< body> Number:'.rand(1,$ limit)。'< / body>< / html>');
}
}
但是当我尝试执行 localhost / app_dev.php / random / 10
出现此错误:
路由文件C:\xampp\htdocs\progetti\Symfony\src\Acme\DemoBundle / Resources / config / routing.yml包含不支持的acme_demo_homepage键:random。预期的资源,类型,前缀,模式,路径,主机,方案,方法,默认值,要求,选项
我认为这是缩进问题。从YAML规格:
在YAML块样式中,结构由缩进确定。
在
一般,缩进定义为一个零个或多个空格字符,
为行的开头。为了保持可移植性,标签字符不能用于缩进,因为不同的系统会对不同的标签进行处理。请注意,大多数现代编辑器可以配置以便按Tab键导致插入适当数量的空格
so:
acme_demo_homepage:
路径:/ hello / {name}
默认值:{_controller :AcmeDemoBundle:默认值:index}
随机:
路径:/ random / {limit}
默认值:{_controller:AcmeDemoBundle:random:index}
或者您可以使用PHP设置路线(这是我的偏好)。例如:
<?php
// src / Acme / DemoBundle / Resources / config /routing.php
使用Symfony\Component\Routing\RouteCollection;
使用Symfony\Component\Routing\Route;
$ collection = new RouteCollection();
#main route
$ collection-> add('_ index',new Route('/ dashboard / index / {page} / {year} / {month}',array
'_controller'=>'AcmeDashboardBundle:默认值:index',
'page'=> 1,
'year'=> date('Y'),
'month'=> date('n'),
)));
return $ collection;
//文件结束
I'm learning Symfony 2 but I have some problems. Using a tutorial, I created this route in the routing.yml
inside bundle:
acme_demo_homepage:
path: /hello/{name}
defaults: { _controller: AcmeDemoBundle:Default:index }
random:
path: /random/{limit}
defaults: { _controller: AcmeDemoBundle:Random:index }
and Eclipse shows me an error at line where defaults
is declared and tells me that :
is unexpected.
I have created the controller:
<?php
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>');
}
}
but when I try to execute localhost/app_dev.php/random/10
this error appears:
The routing file "C:\xampp\htdocs\progetti\Symfony\src\Acme\DemoBundle/Resources/config/routing.yml" contains unsupported keys for "acme_demo_homepage": "random". Expected one of: "resource", "type", "prefix", "pattern", "path", "host", "schemes", "methods", "defaults", "requirements", "options", "condition".
I think this an indentation issue. From YAML Spec:
"In YAML block styles, structure is determined by indentation.
In general, indentation is defined as a zero or more space characters at the start of a line.To maintain portability, tab characters must not be used in indentation, since different systems treat tabs differently. Note that most modern editors may be configured so that pressing the tab key results in the insertion of an appropriate number of spaces. "
So:
acme_demo_homepage:
path: /hello/{name}
defaults: { _controller: AcmeDemoBundle:Default:index }
random:
path: /random/{limit}
defaults: { _controller: AcmeDemoBundle:Random:index }
Alternatively you can set your routes in PHP (it's my preference). For instance:
<?php
//src/Acme/DemoBundle/Resources/config/routing.php
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;
$collection = new RouteCollection();
# main route
$collection->add('_index', new Route('/dashboard/index/{page}/{year}/{month}', array(
'_controller' => 'AcmeDashboardBundle:Default:index',
'page' => 1,
'year' => date('Y'),
'month' => date('n'),
)));
return $collection;
//end of file
这篇关于Symfony2路由不支持的密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!