Joseph
Joseph

Reputation: 2712

Using Route Prefixes in Lumen

From the Lumen 5.2 docs:

The prefix group attribute may be used to prefix each route in the group with a given URI. For example, you may want to prefix all route URIs within the group with admin:

$app->group(['prefix' => 'admin'], function () use ($app) {
    $app->get('users', function ()    {
        // Matches The "/admin/users" URL
    });
});

My code:

$app->group(['prefix' => 'v1'], function () use ($app) {
    $app->get('lessons', function ()    {
        ['as' => 'lessons.index', 'uses' => 'LessonsController@index'];
    });
});

This returns a 200 but it is clearly not calling the index() method on the LessonsController.

I have also tried this:

$app->group(['prefix' => 'v1'], function () use ($app) {
    $app->get('lessons', ['as' => 'lessons.index', 'uses' => 'LessonsController@index']);
});

Results in ReflectionException in Container.php line 738: Class LessonsController does not exist

Upvotes: 2

Views: 2059

Answers (1)

DavidT
DavidT

Reputation: 2481

I am currently using prefixes like this:

$app->group(['namespace' => "App\Http\Controllers", 'prefix' => 'v1'], function($app){
    $app->get('/lessons', 'LessonsController@index');   
});

Which works fine in my version of Lumen. You would access the url /v1/lessons and it is handled by the index() method inside the LessonsController

Note: It would appear that the Lumen documentation misses out that in order to do this you require the 'namespace' => "App\Http\Controllers" key value pair in order for this to work.

Upvotes: 1

Related Questions