2

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

patricus
  • 59,488
  • 15
  • 143
  • 145
Joseph
  • 2,737
  • 1
  • 30
  • 56

1 Answers1

1

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.

DavidT
  • 2,341
  • 22
  • 30
  • I just pasted exactly what you wrote and I still get the error `ReflectionException in Container.php line 738: Class LessonsController does not exist` -- which version of Lumen is it working for you in? – Joseph Feb 25 '16 at 12:49
  • If I just do `$app->get('/v1/lessons', 'LessonsController@index');` without wrapping it in a prefix then it works so the controller is obviously present and correct. – Joseph Feb 25 '16 at 12:50
  • You can try running `composer dump-autoload`. I did however miss out that mine are in a different namespace therefore I also have `'namespace' => "App\Http\Controllers\Backend"` in my array, maybe try `App\Http\Controllers` which you needed when Lumen first came out but they are suppose to have fixed that issue now. – DavidT Feb 25 '16 at 13:29
  • I just did a little testing and you are indeed correct. The documentation appears to be incorrect, and you do require the `namespace` in the array. I have updated my answer to include it – DavidT Feb 25 '16 at 13:54
  • Thanks, that's really great! – Joseph Feb 25 '16 at 17:04