How to define routes in ConfigProvider Module

expressive

#1

Hi,
How can i define routes inside module’s ConfigProvider.php ?
I using expressive 3 with Fast Route.

namespace Auth;

class ConfigProvider
{

    public function __invoke()
    {
         return [
              'dependencies' => $this->getDependencies(),
              'routes' => $this->getRoutes()
         ];
    }

    /** getDependencies() **/

    public function getRoutes() 
    { 
        return [
            'name'  =>  'users',
            'path' => 'api/v1/auth/users',
            'middleware' => ListUsersAction::class,
            'allowed_methods' => ['GET']
        ]
    }
}

#2

You are missing extra array brackets. You return an array and inside that array you have an array for each route.

return [
    [
        'path' => 'api/v1/auth/users',
    ],
    'route.name' => [
        'path' => '/'
    ]
]

More info about autowiring routes: https://docs.zendframework.com/zend-expressive/v3/cookbook/autowiring-routes-and-pipelines/


#3

Sorry, my bad, but I did that and doesn’t work, 404.

/src/Auth/src/ConfigProvider.php

public function getRoutes() : array
    {
        return [
            [
                'name'            => 'users',
                'path'            => 'api/v1/auth/users',
                'middleware'      => UserListAction::class,
                'allowed_methods' => ['GET'],
            ]
        ];
    }

/config/config.php

$aggregator = new ConfigAggregator([

    // Default App module config
    Auth\ConfigProvider::class,

    //... etc
], $cacheConfig['config_cache_path']);

What’s wrong?


#4

Have you set the ApplicationConfigInjectionDelegator? The routes are not injected automatically. You need to use the delegator.