ZF2: ZF1 like URL Routing

ZF2 introduces a new routing for the framework which is similar to ZF1 but has slightly different semantics. The skeleton application ships with a default Application level router which takes a matching controller/action to /application/controller/action URI. If you wish to keep it backward compatible with the ZF1 type of matching, you may want to make the following changes to the module/Application/config/module.config.php file as below:

    'router' => array(
        'routes' => array(
            'home' => array(
                'type' => 'Segment',
                'options' => array(
                    'route'    => '/[:controller[/:action]]',
                    'constraints' => array(
                        'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                    ),
                    'defaults' => array(
                        '__NAMESPACE__' => 'Application\Controller',
                        'controller'    => 'Index',
                        'action'     => 'index',
                    ),
                ),
            ),
        ),
    ),

If you have other modules that match the "module" portion of the URI, don't worry. ZF2 is smart enough to handle multiple matches.

An alternative way to create a similar route is:

            'home' => array(
                'type' => 'Zend\Mvc\Router\Http\Literal',
                'options' => array(
                    'route'    => '/',
                    'defaults' => array(
                        'controller' => 'Application\Controller\Index',
                        'action'     => 'index',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(
                        'default' => array(
                                'type' => 'Segment',
                                'options' => array(
                                        'route' => '[:controller[/:action]]',
                                        'constraints' => array(
                                                'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                                'action' => '[a-zA-Z][a-zA-Z0-9_-]*'
                                        ),
                                        'defaults' => array(
                                                'action' => 'index',
                                                '__NAMESPACE__' => 'Application\Controller'
                                        ),
                                ),
                        ),
                ),
            ),

Share this post

Comments (0)

    No comment

Leave a comment

All comments are moderated. Spammy and bot submitted comments are deleted. Please submit the comments that are helpful to others, and we'll approve your comments. A comment that includes outbound link will only be approved if the content is relevant to the topic, and has some value to our readers.


Login To Post Comment