zend-frameworkrouteszend-framework2zend-router

ZF2 Routing as in ZF1


How can I make the routing automatically work for everything in the ZF1 structure?

module/controller/action/par1Name/par1Val/par2Name/par2Val/

I read the information about routing, but the way I see it, I'd have to add all actions manually, and I see a problem with optional params...


Solution

  • You can set up a wildcard child_route, at least on a per-controller basis, to get zf1-like routes:

    'products' => array(
                    'type' => 'Zend\Mvc\Router\Http\Segment',
                    'options' => array(
                        'route' => '/products[/:action]',
                        'defaults' => array(
                            'controller' => 'Application\Controller\Products',
                            'action' => 'index'
                        )
                    ),
                    'may_terminate' => true,
                    'child_routes' => array(
                        'wildcard' => array(
                            'type' => 'Wildcard'
                        )
                    )
                )
    

    Then you can use, for instance, the url() view helper:

    $this->url('products/wildcard',array('action'=>'edit','id'=>5,'foo'=>'bar');
    

    which will produce a url like /products/edit/id/5/foo/bar