Stefano Maglione
Stefano Maglione

Reputation: 4158

Symfony2 routing unsupported keys

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

Upvotes: 2

Views: 5789

Answers (3)

felipsmartins
felipsmartins

Reputation: 13549

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

Upvotes: 5

Tomasz Madeyski
Tomasz Madeyski

Reputation: 10900

It seems that formatting of your yml file is wrong. You need to mind spaces in yml file (remember not to use tabs instead of spaces) - intendation defines file structure.

Your routing.yml file should look like

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

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

Upvotes: 0

kix
kix

Reputation: 3319

Note that indentation is vital in YAML. If your actual routing.yml looks like what you've posted, then the routing cannot be configured correctly. Here's how it should look:

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

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

Upvotes: 0

Related Questions