razzzila-dev/zf3-oldstyle-router

dev-master 2020-03-30 11:19 UTC

This package is auto-updated.

Last update: 2022-01-29 03:19:11 UTC


README

GitHub license Ask Me Anything ! Contributions welcome

From ZF3 was removed router which find the controller and action using path from request and run it. For my own opinion this is a good thing and you should specify routes more clearly and harder. But still we have the problem when we migrate the codebase from ZF1 to ZF3 iteratively and in the first step we don't want to refactore pathes.

This is the answer - ready "old style" router which authomatically detect if the class and action exist run it. Also, it detect parameters in the query and allow to use them as previously (calling param method from the request).

Installation

Package install

Using Composer

Change working directory to the project root folder and run:

composer require razzzila-dev/zf3-oldstyle-router

Usage

To configure your application for using this router we have to specify it in the router configuration. For doing so, open the your application configs/module.config.php and specify this router:

...

use Zend\Router\RouteInvokableFactory;
use Application\Router\Http\OldStyle as OldStyleRouter;
use Application\Controller\IndexController;

return [
    'router' => [
        'routes' => [
            'default' => [
                'type' => OldStyle::class,
                'options' => [
                    'route'    => '/[:controller[/:action[/]]]',
                    'defaults' => [
                        'controller' => IndexController::class,
                        'action'     => 'index',
                    ]
                ],
            ],
        ],
    ],
    
    'service_manager' => [
        'factories' => [
            OldStyleRouter::class  => RouteInvokableFactory::class,
        ]
    ]
];

Under "defaults" section you can find the "controller" and "action" parameters. Here you can specify a fallback controller and action in case if controller and action from the request didn't found. To specify which module controllers will be used for searching in, you can use "module" option. By default this is "Application" module which means that for request "/index/index" we will search and trigger "Application\Controller\IndexController@indexAction".

License

MIT