0

With this code:

$frontController = Zend_Controller_Front::getInstance(); 
$router = $frontController->getRouter();
$router->addRoute(
    'test',
    new Zend_Controller_Router_Route(
        '/test/:action/:type/:id',
        array(
            'controller' => 'admin'
        )
    )
);

http://app/test/param1/param2/param3 -> OK

http://app/test/param1/param2/ -> FAIL

In the second case the application don't recognize param2.

It seems that the application needs the param3 in order to read the param2...

How can I do it?

Thanks!


Test with the code from @RageZ

$frontController = Zend_Controller_Front::getInstance(); 
$router = $frontController->getRouter();
$router->addRoute(
    'test',
    new Zend_Controller_Router_Route(
        '/test/:action/:type/:id',
        array(
            'controller' => 'admin',
            'id' => 0
        ),
        array(
            'id' => '\d+'
        )
    )
);

http://app/test/ -> OK

http://app/test/some -> OK

http://app/test/some/more -> FAIL

http://app/test/some/more/andmore -> OK

Ideas?

Carter Allen
  • 1,984
  • 15
  • 22
joanballester
  • 207
  • 3
  • 11

2 Answers2

1

You have to provide a default value if the parameter is optional.

$frontController = Zend_Controller_Front::getInstance(); 
$router = $frontController->getRouter();
$router->addRoute(
    'test',
    new Zend_Controller_Router_Route(
        '/test/:action/:type/:id',
        array(
            'controller' => 'admin',
            'id' => 0
        ),
        array(
            'id' => '\d+'
        )
    )
);

Nothing to do with your question but it's a good practice to use the third param of addRoute. Zend Framework would verify that the parameters value match the format you have specified, in that case I suppose id is an integer.

RageZ
  • 26,800
  • 12
  • 67
  • 76
1

try giving a default value to everything

$frontController = Zend_Controller_Front::getInstance(); 
$router = $frontController->getRouter();
$router->addRoute(
    'test',
    new Zend_Controller_Router_Route(
        '/test/:action/:type/:id',
        array(
            'controller' => 'admin',
            'action' => 'index',
            'type' => 'sometype',
            'id' => 0
        ),
        array(
            'id' => '\d+'
        )
    )
);

Just tried your code in some test project and this fixed it hope it works for you!

Iznogood
  • 12,447
  • 3
  • 26
  • 44