2

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

2
  • Is your routing.yml exactly like one you posted? You need to have path and defaults keys intended using spaces Commented Sep 30, 2014 at 11:51
  • Yes my routing.yml is like my posted code and this is its directory path :Symfony\src\Acme\DemoBundle\Resources\config\routing.yml @TomaszMadeyski Commented Sep 30, 2014 at 11:58

3 Answers 3

5

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
Sign up to request clarification or add additional context in comments.

5 Comments

Ok, now at execution time there is no error and app works correctly but my Eclipse continue to show me the same error @felipsmartins
The Symfony Book has PHP routing examples, but it still leaves a lot to be desired. If you want to learn more about routing with PHP (as opposed to PHP Annotations, YAML, or XML) then see the Routing Component Reference.
@thohl I think you have commented on wrong place, dont't you? ^^
@felipsmartins I wanted to stick it here. I found your answer and I agree with you about using PHP for routes. I google'd my way into the Symfony Book several times, but the Routing Component Reference has much more detail. It took me a while to find it though -- just hoping to save someone else some time.
@thohl yep! I agree. The Symfony components documentation is much better and it makes sense since components is intended to work as standalone pieces thus details is important.
0

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 }

Comments

0

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 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.