0

I want this route:

localhost/events/ -> show list of events (listAction)
localhost/events/ultra-music-festival -> show event detail
localhost/events/bob-ross -> show event detail
localhost/events/anyAction -> execute any action in EventsController.php
localhost/events/anyAction/23 -> execute any action, with optional parameter (13), in EventsController.php

My code:

'router' => array(
    'routes' => array(
        'events' => array(
            'type' => 'literal',
            'options' => array(
                'route' => '/events',
                'defaults' => array(
                    '__NAMESPACE__' => 'Events\Controller',
                    'module' => 'Events',
                    'controller' => 'events',
                    'action' => 'list'
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'default' => array(
                    'type' => 'segment',
                    'options' => array(
                        'route' => '[/:action[/:id]]',
                        'constraints' => array(
                            'action' => '[a-zA-Z][a-zA-z0-9_-]*',
                            'id' => '[a-zA-z0-9_-]*', // slug
                        ),
                    ),
                ),
                'detail' => array(
                    'type' => 'segment',
                    'options' => array(
                        'route' => '[/:id]',
                        'defaults' => array(
                            'action' => 'detail'
                        ),
                        'constraints' => array(
                            'id' => '[a-zA-z0-9_-]*', // slug
                        )
                    )
                ),
                'paginator' => array(
                    'type' => 'Segment',
                    'options' => array(
                        'route' => '[/:action]/page/[:page]',
                        'constraints' => array(
                            'page' => '\d+',
                        ),
                    )
                ),
            ),
        )
    )
),

This route works perfectly in these situations:

localhost/events/ -> show list of events (listAction)
localhost/events/ultra-music-festival -> show event detail
localhost/events/detail/ultra-music-festival -> show event detail
localhost/events/anyAction/23 -> execute any action, with optional parameter (13), in EventsController.php

This route does NOT work in these situations:

localhost/events/anyAction -> execute any action in EventsController.php

I have not found any solution to this Slug use case, and that's what I need to do. For me this type of route is (or should be) very used. For example, in case of a blog:

/blog/postname1
/blog/postname2
/blog/create
/blog/edit/1
/blog/edit/2
Tenil
  • 11
  • 3

1 Answers1

0

Try to change this:

'route' => '[/:action[/:id]]',

to:

'route' => '/:action[/:id]',

The additional brackets are causing the issue.

Wilt
  • 41,477
  • 12
  • 152
  • 203
  • Unfortunately not. An ID (numeric) is not the same as a SLUG (letters). When it comes to ID the difference is clear, number and letters. When it is a SLUG, everything is letter, both the slug and the actions. – Tenil Dec 30 '16 at 21:31