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