phpzend-framework2zend-routezend-router

ZendFramework 2 - How to make all the actions available with one router rule? Its only allowing action index


How to allow all sub actions inside that controller with one router rule? For example this follow:

visit: site/login                - works only
       site/login/forgetpassword - does not work
       site/login/remmeberme     - does not work

Example:

$router = $e->getApplication()->getServiceManager()->get('router');
$route = Http\Literal::factory(array(
  'route' => '/login',
  'defaults' => array(
    'controller' => 'Application\Controller\Login',
    'action' => 'index'
  ),
));
$router->addRoute('login', $route, null);

Follow up:

How can i make it so that /login and /login/anything works?

$route = Http\Segment::factory(array(
  'route' => '/login[/:action]',
  'defaults' => array(
    'controller' => 'Application\Controller\Login',
    'action' => 'index'
  ),
));
$router->addRoute('login', $route, null);

Solution

  • There is an excellent QuickStart Tutorial available within the official Documentation. Set up your route like the following to be allowed multiple actions and an ID Parameter. Fur further information please take a look at the documentation.

    You may also be interested in DASPRiDs presentation from ZendCon2012

     'router' => array(
        'routes' => array(
            'album' => array(
                'type'    => 'segment',
                'options' => array(
                    'route'    => '/album[/:action][/:id]',
                    'constraints' => array(
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id'     => '[0-9]+',
                    ),
                    'defaults' => array(
                        'controller' => 'Album\Controller\Album',
                        'action'     => 'index',
                    ),
                ),
            ),
        ),
    ),