Search code examples
phpfiltercodeigniter-4nested-routesphp-8.1

Nesting Groups of Filters not working in CodeIgniter 4


I'm having hard time to configure nested groups in CI 4. I'm trying to configure the access to routes passing thru a group of filters to authenticate and validate the data that i'm using in the controller.

Here's a example of the configuration:

$routes->group('api/', ['filter' => 'jwt'], function ($routes) {
    $routes->group('', ['filter' => 'scopefilter:test'], function ($routes) {
        $routes->post('test', 'Api\Test::index');
    });
});

In this case, the only filter that worked is the scopefilter. The jwt is invisible. The expecting behavior was that the request should pass thru jwt filter and than the scopefilter.

Has anyone already solved this?


Solution

  • According to the CodeIgniter Docs:

    Options passed to the outer group() (for example namespace and filter) are not merged with the inner group() options.

    As your inner group has no actual route definition, why not insert the filter in the outer group like so:

    $routes->group('api/', ['filter' => ['jwt', 'scopefilter:test']], function ($routes) {
        $routes->post('test', 'Api\Test::index');
    });
    

    The filters are getting called in the order they are assigned.

    Update due to IF Ferreiras comment: For this to work you have to enable multiple filters in Config/Feature.php:

    public bool $multipleFilters = true;