zendframework / zend-expressive-skeleton

Begin developing PSR-7 middleware applications in seconds!
BSD 3-Clause "New" or "Revised" License
136 stars 90 forks source link

Default delegator addition #223

Closed adamculp closed 5 years ago

adamculp commented 6 years ago

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?

geerteltink commented 6 years ago

I'm using this in a few projects as well. But I'm not really liking it for the default application, especially if you have a lot of routes. I prefer to have the default application routes in the config/routes.php file as it is in the release-3.0.0 branch.

For modules this solution is perfect though. I would rather see this in the zend-expressive-tooling package as an addition to creating a new module.

adamculp commented 6 years ago

I can agree with that.

I think including it in the tooling for module:create, and possibly middleware:create, could be a good step.

geerteltink commented 5 years ago

Closing this in favor of https://github.com/zendframework/zend-expressive-tooling/issues/57