ZF2 - Issues with $this->url and Routing

517 views Asked by At

I'm having and issue with the view helper function this->url() that doesn't return and url from a child route and I get a White Screen of the dead. When I put the url manually in the browser the router recognizes the url well.

I have this in my module.config.php:

'news' => array(
            'type' => 'Segment',
            'options' => array(
                'route' => '/news[/:id]',
                'constraints' => array(
                    'id'    => '[0-9]+',
                    ),
                'defaults' => array(
                    'controller' => 'Application\Controller\News',
                    'action' => 'index',
                    ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'allnews' => array(
                    'type' => 'Segment',
                    'options' => array(
                        'route' => '[/:action[/:tab[/:page]]]',
                        'constraints' => array(
                            'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                            'tab'    => '[0-9]+',
                            'page'     => '[0-9]+',
                        ),
                        'defaults' => array(
                            'controller' => 'Application\Controller\News',
                            'action' => 'all',
                            'tab' => 1,
                            'page' => 1,
                        ),

                    ),
                ),
            ),
        ),

For example:

/news/2

Works correctly and matches the parent route

And:

/news/all/2/5

Works correctly and matches the child route.

But when I use this in the view-helper I get a White Screen of the Dead:

 echo($this->url('news/allnews', array('action' => 'all', 'tab' => '1', 'page' => '1')));

My question is: What it's wrong?? I used this method in others views and worked well.

2

There are 2 answers

1
cptnk On BEST ANSWER

You do not need to use child routes in combination with Router Segments. One Segment should be sufficient!

'news' => array(
   'type' => 'Segment',
   'options' => array(
      'route' => '/news[/:action[/:id][/:tab][/:page]]',
      'constraints' => array(
          'id'    => '[0-9]+',
          'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
          'tab'    => '[0-9]+',
          'page'     => '[0-9]+',
       ),
       'defaults' => array(
          'controller' => 'Application\Controller\News',
          'action' => 'index',
       ),
    ),
 ),

As long as the Defaultcontroller is sufficient you for your needs you also would not need to have it within your params. Once you need more then 1 you prob want to have another constraint defining the controller.

Now you could simply use the url viewhelper in various ways.

if you only need the default action within your default controller you could write it like so.

$this->url('news');

If you need to route to actions with params you could use the viewhelper like so

$this->url('news', array('action' => $action, 'tab' => $tab, 'page' => $page));

Ideally I would use a Router Literal with Router Segments as child routes since bombarding the router segment with tons of constraints gets messy pretty fast.

         ...
         'custom_route' => array(
            'type'    => 'Literal',
            'priority' => 1337,
            'options' => array(
                'route'    => '/custom',
                'defaults' => array(
                    '__NAMESPACE__' => 'Custom\Controller',
                    'controller'    => 'Index',
                    'action'        => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'custom_child' => array(
                    'type'    => 'Segment',
                    'options' => array(
                        'route'    => '/[:controller[/:action]][/:id]',
                        'constraints' => array(
                            'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                            'action'      => '[a-zA-Z][a-zA-Z0-9_-]*',
                            'id'           => '[0-9]+',      
                        ),
                        'defaults' => array(
                        ),
                    ),
                ),
            ),
        ),
      ...
1
Purple Hexagon On

Pretty sure this won't help but everything does look right in your routing... Have you tried

$this->url('news/allnews', array('action' => 'all', 'tab' => 1, 'page' => 1))

Don't know how the routing stuff handles contraints on digits and providing strings to url view helper.

You probably either need to enable errors or look in the log to find out the actually error