Zend Framework 2 Target Segment

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.

+4
source share
5 answers

I played with him a little more. After debugging, I still could not understand. I tried adding a default value to the code parameter, and it seems to be a trick. Why on Earth, which works and how it makes sense, I have no idea. It doesn't matter to me whether the default value is specified or not, if I specified the value in the URL for the parameter in the URL. Nevertheless, it seems to matter, or at least in this case.

Here is the working code. I also updated the regex 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, ), ), ), ), ), 
+4
source

My solution: set the default for your optional parameter to an empty string

I had a similar problem, I solved it by setting the default value for an optional parameter (in your case, "username") to an empty string ""; My decision:

 'users' => array( 'type' => 'segment', 'options' => array( 'route' => '/users[/:user_id]', 'constraints' => array( 'user_id' => '[0-9]*', ), 'defaults' => array( 'controller' => 'My\Controller\User', 'user_id' => "" ) ), 
+4
source

The second exception that you get Missing parameter "code" comes from the assembly, not for matching. This means that you did not deliver the code when building the profile/profile_view .

By the way, you do not need the prefix child_routes with the name of the parent routes. Just call the "view" of the children's route and assemble it as a profile/view .

+1
source

I believe the may_terminate option is may_terminate , for example:

 '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( ... 
0
source

Two reasons this happens according to Bittarman. According to him, the main reason is as follows:

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

I had the smallest reason: I used $ this-> url () in my layout. Since there was no route match - 404 error - it exploded on all 404 errors.

 <Bittarman> for all of those things, stop doing if (!$this->url() == $this->url('home') etc <ezio> ummm when it 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 
0
source

All Articles