phpzend-framework2zend-route

Zend routing issue with dynamic segment


I'm working on a Zend project and I have some issues with the router/routes.

I want the have these URLs:

  1. www.mydomain.tld/contact/person
  2. www.mydomain.tld/contact/person/formsend
  3. www.mydomain.tld/contact/person/id

The third segment in the URL will be a fixed text "formsend" or it will contain an ID, which is a number like '123456789'. URL 1 and 3 should both execute the indexAction(), URL 2 must execute the sendAction().

Now I have these route's setup to get URL 1 and 2 working:

return array( 
    'router' => array(
        'routes' => array(
        'contact' => array(
            'type' => 'Segment',
            'options' => array(
                'route' => '/contact[/][:dns]',
                'defaults' => array(
                    '__NAMESPACE__' => 'project\Controller',
                    'controller' => 'Contact',
                    'action' => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'send' => array(
                    'type' => 'Segment',
                    'options' => array(
                        'route' => '[/[:action[/]]]',
                        'defaults' => array(
                          'action' => 'send'
                        ),
                    ),
                ),
             ),
        ),

I have no idea how I must change my routes so I would be able to have segment_3 inside the indexAction(). What do I need to change?


Solution

  • It sounds to me you only need two routes:

    First define /contact/:type/formsend, then /contact/:type[/:id].

    You can make them children of a top-level (non-terminating) /contact or /contact/:type route, or not.