1

I am currently trying to configure a routing option in Symfony2 so /cms will route to /cms/role/view. However, the passing of defaults doesn't seem to work properly.

/src/MyProject/CMSBundle/Resources/config/routing.yml

MyProjectCMS_specific:
pattern:  /cms/{page}/{option}
defaults: { _controller: MyProjectCMSBundle:Main:index, page: role, option: view }
requirements:
    _method: GET

/src/MyProject/CMSBundle/Controller/MainController.php

<?php
    namespace MyProject\CMSBundle\Controller;

    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

    class MainController extends Controller
    {
        public function indexAction($page, $option)
        {
            $response = null;

            /* Switch statement that determines the page to be loaded. */

            return $response;
        }

    }
?>

The problem is that when I try to go to `localhost/app_dev.php/cms', it gives me the following error:

Controller "MyProject\CMSBundle\Controller\MainController::indexAction()" requires that you provide a value for the "$page" argument (because there is no default value or because there is a non optional argument after this one).
500 Internal Server Error - RuntimeException

However, if I try to visit localhost/app_dev.php/cms/role or localhost/app_dev.php/cms/role/view, it gives me the correct page. I've tried adding a default route to /cms, but it still gives me the same error. How is this possible and how can I fix this?

Thanks in advance.

Rex
  • 610
  • 9
  • 19
  • First of all you should decide between routing by yaml or by annotation. Depending on your configuration one is not used and only confuses readers and clutters the code! – Sgoettschkes Feb 26 '12 at 14:47
  • Sorry, you're right. I added it because I tried if it would make a difference, but it didn't. I removed it now. – Rex Feb 26 '12 at 15:59

1 Answers1

2

I don't know what is the difference between what you wrote and

    public function indexAction($page = "role", $option = "view")

but maybe you could try it and tell us.

greg0ire
  • 22,714
  • 16
  • 72
  • 101
  • I set the defaults in routing.yml. According to the Symfony2 manual, that should work, but it doesn't work if I give no arguments at all. Anyway, your suggestion actually fixed my problem, so thank you for that. It's not how I prefer to fix this but it's better than nothing. ;) – Rex Feb 26 '12 at 16:05
  • Yeah sure, it's not an explanation, just a workaround. But I don't think it deserves an anonymous, unexplained downvote... – greg0ire Feb 26 '12 at 16:38
  • My eternal gratefulness should be enough. – Rex Feb 26 '12 at 17:01