I'm trying to make a route (or two) that will allow me to invoke two different actions with the following URL formats.
mydomain.com/profile mydomain.com/profile/1234/something
For the second format, 1234 must be a required integer value, and something an optional string. The first format is simple using a literal route. I thought I could add a child segment route for the second format, but I cannot get it to work. I tried to leave the first format and make only the second with a segmented route, but I did not achieve this 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 error message:
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. For example, "/: foo [/: bar]" will match "/" followed by text, and assign this to the key "foo"; if any additional "/" characters are found, any text following the last will be assigned to the "bar" key.
Isn't that what I'm doing? The above errors remain the same if I comment on the limitations.
What am I missing here? Thanks in advance.