Symfony2路由不支持的密钥

编程入门 行业动态 更新时间:2024-10-17 23:27:11
本文介绍了Symfony2路由不支持的密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在学习Symfony 2,但我有一些问题。使用教程,我在 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路由不支持的密钥

本文发布于:2023-10-31 13:22:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1546206.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:密钥   不支持   路由

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!