I am trying to make a route (or two) that will let me invoke two different actions with the below URL formats.
mydomain.com/profile
mydomain.com/profile/1234/something
For the second format, the 1234
should be a required integer value, while something
should be an optional string. The first format is simple by using a literal route. I thought I could add a segment child route for the second format, but I cannot get it to work. I tried to leave out the first format and only do the second one with a segment route, but I wasn't successful in that either.
Here is what I tried:
'profile' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/profile',
'defaults' => array(
'controller' => 'User\Controller\User',
'action' => 'profile'
)
),
'child_routes' => array(
'profile_view' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/:code[/:username]',
'constraints' => array(
'code' => '[0-9]*',
'username' => '[a-zA-Z0-9_-]*'
),
'defaults' => array(
'controller' => 'User\Controller\User',
'action' => 'view_profile'
)
)
)
)
)
For mydomain.com/profile
, I get the following errror:
Fatal error: Uncaught exception 'Zend\View\Exception\RuntimeException' with message 'No RouteMatch instance provided'
For mydomain.com/1234/something
, I get the following error:
Fatal error: Uncaught exception 'Zend\Mvc\Router\Exception\InvalidArgumentException' with message 'Missing parameter "code"'
The manual states the following:
If a segment is optional, it should be surrounded by brackets. As an example, “/:foo[/:bar]” would match a “/” followed by text and assign it to the key “foo”; if any additional “/” characters are found, any text following the last one will be assigned to the key “bar”.
Is that not exactly what I am doing? The above errors remain the same if I comment out the constraints.
What am I missing here? Thanks in advance.
My solution: Set the default for your optional parameter to empty string
I ran into a similar problem, I solved it by setting the default value for the optional parameter (in your case "username" to an empty string ""; My solution:
'users' => array(
'type' => 'segment',
'options' => array(
'route' => '/users[/:user_id]',
'constraints' => array(
'user_id' => '[0-9]*',
),
'defaults' => array(
'controller' => 'My\Controller\User',
'user_id' => ""
)
),
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With