duanbing2963 2014-09-30 11:49
浏览 64
已采纳

Symfony2路由不受支持的密钥

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

  • 写回答

3条回答 默认 最新

  • doushihu5475 2014-09-30 11:59
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)