gpt4 book ai didi

php - Symfony2 路由不支持的键

转载 作者:行者123 更新时间:2023-12-04 15:23:41 25 4
gpt4 key购买 nike

我正在学习 Symfony 2,但遇到一些问题。使用教程,我在 bundle 内的 routing.yml 中创建了此路由:

acme_demo_homepage:
path: /hello/{name}
defaults: { _controller: AcmeDemoBundle:Default:index }

random:
path: /random/{limit}
defaults: { _controller: AcmeDemoBundle:Random:index }

并且 Eclipse 在声明 defaults 的行显示了一个错误,并告诉我 : 是意外的。

我已经创建了 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>');
}

}

但是当我尝试执行localhost/app_dev.php/random/10时出现此错误:

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".

最佳答案

我认为这是一个缩进问题。来自 YAML 规范:

"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
. "

所以:

acme_demo_homepage:
path: /hello/{name}
defaults: { _controller: AcmeDemoBundle:Default:index }

random:
path: /random/{limit}
defaults: { _controller: AcmeDemoBundle:Random:index }

或者,您可以在 PHP 中设置路由(这是我的偏好)。例如:

<?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

关于php - Symfony2 路由不支持的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26120106/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com