Reputation: 2160
I have created a basic Zend Framework 2 application using the Skeleton Application and Module although I have encountered a routing problem and can't seem to work around it.
Within my module, which is loosely based on the new Quickstart module on the Zend Framework web site, I have the following route within module.config.php
'router' => array(
'routes' => array(
'blog' => array(
'type' => 'Literal',
'options' => array(
// Change this to something specific to your module
'route' => '/blog',
'defaults' => array(
// Change this value to reflect the namespace in which
// the controllers for your module are found
'__NAMESPACE__' => 'Blog\Controller',
'controller' => 'View',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
// This route is a sane default when developing a module;
// as you solidify the routes for your module, however,
// you may want to remove it and replace it with more
// specific routes.
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '[/:controller[/:action]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
),
),
),
),
),
),
),
This route appears to work fine when using a standard module, controller and action URL request:
blah.com/blog/post/view
Where I am having problems is when I try to pass some data over the URL:
blah.com/blog/post/view/id/1
The latter of the two URLs results in a 404. I am guessing I need a child route to allow for data to be passed on the URL, although I am stuck on exactly what I would need. Is anyone able to point me in the right direction? I have browsed the reference guide, although I can't seem to get across the line.
Any help appreciated.
Upvotes: 0
Views: 931
Reputation: 360
In case you need multiple parameters which are optional and can be provided in any random combination (e.g. pageNumber = 2, participation = active), you need to add a child route of type query and with this you can provide GET parameters completely flexible:
The url fitting the following example can be build with the url view helper using this syntay: $this->url('event/index/query', array('o_id' => 1, 'participation' => 'active','pageNumber' => '2'));
'routes' => array(
'event' => array(
'type' => 'literal',
'options' => array(
'route' => '/event',
'defaults' => array(
'controller' => 'cebEvent.eventController',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'index' => array(
'type' => 'segment',
'options' => array(
'route' => '/index[/:o_id]',
'defaults' => array(
'controller' => 'eventController',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'query' => array(
'type' => 'Query',
),
),
),
Upvotes: 2
Reputation: 508
You have to add optional segments to your route. Also add default values for it: try this:
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '[/:controller[/:action[/id[/:idvar]]]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'idvar' => '0'
),
),
),
and within your controller action, try:
$id = $this->params('idvar')
;
to retrieve the idvar
value.
Upvotes: 2