Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

callam's avatar

Nested Namespace Routing

I like to group my routes by namespace, prefix etc. to make the routes file modular and not too overwhelming. I recently began writing an API in Laravel and discovered Lumen so decided to port it over.

I had already begun organising my API routes using groups and found when transferring them to Lumen, the group attributes weren't being merged with the parent group's attributes.

Looking under the hood, the group method of the Application class does indeed skip the merging process which overwrites the parent attributes. Although this may be an intended change, in the documentation, nested namespaces are used http://lumen.laravel.com/docs/routing#route-group-namespaces

So my question is, which is wrong; the group method or the documentation? And in any case should this issue be raised or does someone know something?

0 likes
5 replies
thomaskim's avatar

@callam Good catch. I believe the documentation is wrong. I sent in a pull request to fix that so we'll see if it gets merged.

I believe Lumen purposely does not merge attributes. Nested groups aren't really supported.

Also, when you use the route group namespace, I believe you need to specify the full path like so:

$app->group(['namespace' => 'App\Http\Controllers\Admin'], function ($app) {

    // Controllers Within The "App\Http\Controllers\Admin" Namespace

});
callam's avatar

@thomaskim Okay, thanks for sending that pull request.

I was thinking of creating an extension to the Lumen Application that uses the group stack implementation used in Laravel's Router. What are your thoughts on that?

callam's avatar
callam
OP
Best Answer
Level 2

This is what I've come up with, works very nicely.

use Closure;
use Illuminate\Routing\Router;
use Laravel\Lumen;

class Application extends Lumen\Application
{
    public function group(array $attributes, Closure $callback)
    {
        $router = app(Router::class);

        $attributes = $router->mergeGroup($attributes, (array) $this->groupAttributes);

        parent::group($attributes, $callback);
    }
}
1 like

Please or to participate in this conversation.