Question

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.

Was it helpful?

Solution 2

I played around with it some more. After debugging, I still couldn't figure it out. I tried to add a default value to the code parameter and that seemed to do the trick. Why on Earth that works and how it makes sense, I have no idea. To me it shouldn't matter if I have a default value specified or not if I have provided a value in the URL for the parameter in the URL. Nevertheless, apparently it does matter - or at least in this case it did.

Here is the working code. I also updated the regular expression for the code parameter.

'profile' => array(
    'type' => 'Zend\Mvc\Router\Http\Literal',
    'options' => array(
        'route' => '/profile',
        'defaults' => array(
            'controller' => 'User\Controller\User',
            'action'     => 'profile',
        ),
    ),
    'may_terminate' => true,
    'child_routes' => array(
        'view' => array(
            'type' => 'Zend\Mvc\Router\Http\Segment',
            'options' => array(
                'route' => '/:code[/:username]',
                'constraints' => array(
                    'code' => '\d+',
                    'username' => '[a-zA-Z0-9_-]*',
                ),
                'defaults' => array(
                    'controller' => 'User\Controller\User',
                    'action' => 'viewProfile',
                    'code' => 0,
                ),
            ),
        ),
    ),
),

OTHER TIPS

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' => ""
        )
    ),

The second exception you get Missing parameter "code" comes from assembling, not matching. This means that you didn't supply the code when assembling the route profile/profile_view.

By the way, you don't have to prefix your child_routes with the parent routes' name. Just call the child route "view", and assemble it as profile/view.

I believe you are missing the may_terminate option, like so:

'profile' => array(
    'type' => 'Zend\Mvc\Router\Http\Literal',
    'options' => array(
        'route' => '/profile',
        'defaults' => array(
            'controller' => 'User\Controller\User',
            'action' => 'profile'
        )
    ),
    'may_terminate' => true,
    'child_routes' => array(
    ...

Two reasons this occurs according to Bittarman. The main reason, he says, is:

<Bittarman> ezio, that looks like what happens when you make a new phprenderer rather than using the configured one

I had the lesser of the reason apparently: I was using $this->url() in my layout. Because there was no route match--a 404 error--it was blowing up on all 404 errors.

<Bittarman> for all of those things, stop doing if (!$this->url() == $this->url('home') etc
<ezio> ummm when it's home my business partner wants the whole order changed so google can hit on a bunch of keyords
<Bittarman> instead, in the VIEW for home, set what you need.
<ezio> how would i set the second example where i'm trying to highlight the currently-being-used menu item
<Bittarman> so, in your layout, you just want echo $this->headTitle()->setSeparator(' - ')->setAutoEscape(false)
<Bittarman> ezio, use the placeholder view helper
<ezio> okay
<ezio> thanks Bittarman ... tearing my hair out is not fun when you're already losing so much of it :p
<Bittarman> <?= $this->placeholder('currently-being-used') ?>
<Bittarman> and in your view, <?php $this->placeholder('currently-being-used')->append('foo') ?>
<ezio> sense making a lot you are
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top