zend-expressive-tooling icon indicating copy to clipboard operation
zend-expressive-tooling copied to clipboard

Create RoutesDelegator when creating a new module

Open geerteltink opened this issue 7 years ago • 1 comments

As requested by @adamculp

I've grown accustomed to using a RoutesDelegator.php in my v2 Expressive projects, and specified it as follows in the ConfigProvider.php

public function getDependencies()
    {
        return [
            'delegators' => [
                \Zend\Expressive\Application::class => [
                    RoutesDelegator::class,
                ],
            ],
            'invokables' => [
            ],
            'factories'  => [
            ],
        ];
    }

When creating an Expressive driven modular application it makes module specific routes easier to maintain. (Not sure of the implications for a flat application.)

What are the thoughts of including this in the skeleton starting with v3?

So, other than adding the 'delegators' key to the ConfigProvider.php as part of the getDependencies() return, we could add the RouteDelegator.php with something like:

namespace App;

use App\Handler;
use Psr\Container\ContainerInterface;
use Zend\Expressive\Application;

class RoutesDelegator
{
    /**
     * @param ContainerInterface $container
     * @param string $serviceName Name of the service being created.
     * @param callable $callback Creates and returns the service.
     * @return Application
     */
    public function __invoke(ContainerInterface $container, $serviceName, callable $callback)
    {
        /** @var $app Application */
        $app = $callback();

        // Setup routes:
        $app->get('/', Handler\HomePageHandler::class, 'home');
        $app->get('/api/ping', Handler\PingHandler::class, 'api.ping');

        return $app;
    }
}

Thoughts?

For details and brief discussion see zendframework/zend-expressive-skeleton#223

geerteltink avatar Feb 15 '18 07:02 geerteltink

This repository has been closed and moved to mezzio/mezzio-tooling; a new issue has been opened at https://github.com/mezzio/mezzio-tooling/issues/2.

weierophinney avatar Dec 31 '19 21:12 weierophinney