0

I am using ZendFramework 2.x and trying to add a route to some existing ones. Moreover I want to put some parameters in die URL as well. If I use the segment-type for my new route ('showroom') I am able to call my new URL and will get forwarded to the corresponding view. Unfortunately I am not able to set some parameters in the URL. The other option is to use segment type in my module.config.php-file, but I will get some ZF-2-Exception, that my route is not configured correctly, this happens even before rendering my intro-view. Thanks in advance for showing me, how to combine segment and literal-type usage in route-child-route-combination or for telling me how to add parameters to literal type URLs.

Route-configuration in module.config.php of Module:

'router' => array(
    'routes' => array(
        'zfcuser' => array(
            'type' => 'Literal',
            'priority' => 1000,
            'options' => array(
                'route' => '/user',
                 'defaults' => array(
                     'controller' => 'zfcuser',
                     'action'     => 'index',
                 ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'showroom' => array(
                    'type' => 'Segment',
                    'options' => array(
                        'route'    => '/:id',
                        'defaults' => array(
                            'controller' => 'zfcuser',
                            'action'     => 'showRoom',
                            'id'         => '1',
                        ),
                    ),
                ),
                'login' => array(
                    'type' => 'Literal',

UPDATE: snippet which triggers the error (see comments)

<?php 
    $roomIndex = 1;
    foreach($roomsPaginator->getCurrentItems() as $room){
        $roomURL = 'zfcuser/showroom' . '/' . $roomIndex;
        echo "<p>Name: " . $room['name'] . "; Luftfeuchtigkeit: " . $room['humidity'] . ";  <a class='btn' href='" . $this->url($roomURL) . "'>Betreten &raquo;</a></p>";
        $roomIndex++;
    }
?>
Jochen
  • 1,746
  • 4
  • 22
  • 48
  • Your code looks fine. When you say you're not able to set parameters in the URL, could you provide more info? How are you doing this, and what error/problems are you having? – Tim Fountain Jul 29 '13 at 20:14
  • @TimFountain if I set the type of my route to literal and send a request to mydomain.tld/showroom/myparameter - I get a 404 error. – Jochen Jul 29 '13 at 20:19
  • If it has a parameter it should be a Segment route. What error do you get with the config in your question? – Tim Fountain Jul 29 '13 at 20:47
  • @TimFountain I got this error message: Route with name "showroom" does not have child routes – Jochen Jul 29 '13 at 20:50
  • Can you post the code that causes that error? – Tim Fountain Jul 29 '13 at 20:56

1 Answers1

0

I'm not sure if this is the cause of your error, but I don't think you're using the URL helper correctly. You don't build a URL and pass it to the helper, the idea is the helper builds the URL for you using the route name and params. You probably want something like this:

$roomIndex = 1;
foreach($roomsPaginator->getCurrentItems() as $room){
    echo "<p>Name: " . $room['name'] . "; Luftfeuchtigkeit: " . $room['humidity'] . ";  <a class='btn' href='" . $this->url('showroom', array('id' => $roomIndex)) . "'>Betreten &raquo;</a></p>";
    $roomIndex++;
}
Tim Fountain
  • 33,093
  • 5
  • 41
  • 69
  • Unfortunately, this did not help. Anyway I have another idea which causes the problem: the view-file resides in my application-module, the route-definition is only in my zfcuser-module. Currently after your changes I still get the error, that the route showroom is not defined. – Jochen Jul 29 '13 at 21:36
  • Great it worked now - I just needed to define the route in application-module as well. Thanks a lot! – Jochen Jul 29 '13 at 21:42