6
votes

I have declared a route group in laravel/lumen like so:

$app->group(['middleware' => 'auth'], function () use ($app) {
    $app->get('/details', 'UserController@details');
});

all contents of route file web.php are like so:

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It is a breeze. Simply tell Lumen the URIs it should respond to
| and give it the Closure to call when that URI is requested.
|
*/

$app = app();

$router->get('/', function () use ($router) {
    return $router->app->version();
});


$app->group(['middleware' => 'auth'], function () use ($app) {
    $app->get('/details', 'UserController@details');
});

on making a call to http://THE_URL/

I get an error Call to undefined method Laravel\Lumen\Application::group()

ERROR

How do I add route group with a middleware ?

3
Did you check this? lumen.laravel.com/docs/5.2/routing#route-groups although it doesn't seem to be different from what you did.Oluwatobi Samuel Omisakin
wait! do you have to add those $app = app(); $router->get('/', function () use ($router) { return $router->app->version(); }); ?Oluwatobi Samuel Omisakin
@OmisakinOluwatobi Yes I checked the URL and the part you mentioned in the second comment is the default route defined in lumen also I did add $app = app(); as it was not foundShakti Phartiyal
No, I think thats where you have a problem. You don't need to create the $app variable. Laravel knows how they managed to create that so you can use. At least on my lumen project, this variable is ready for use.Oluwatobi Samuel Omisakin
no post the solution here and mark it as answeredOluwatobi Samuel Omisakin

3 Answers

5
votes

Actually as @Aine said in Lumen 5.5+ you should change:

LumenPassport::routes($this->app);

to

LumenPassport::routes($this->app->router);

Application does not have group() method anymore.

thanks

3
votes

I just ran into the same problem, with upgrading Lumen from 5.4 to 5.5

In the docs, it states:

Updating The Routes File

After updating your bootstrap/app.php file, you should update your routes/web.php file to use the $router variable instead of the $app variable:

$router->get('/hello', function () { return 'Hello World'; });

For me, this meant lots of updating, where ever $app was used in the routing.

$app->get()                     => $router->get()
$app->group()                   => $router->group()
$app->post()/patch()/delete()   => $router->post()/patch()/delete()

// Inside functions    
$app->group([...], function () use ($app)
=>
$router->group([...], function () use ($router)

// And even in some other files
$app = app();
$app->get();
    =>
$app = app();
$router = $app->router;
$router->get();

Hope this helps.

1
votes

Found the solution to the problem:

<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It is a breeze. Simply tell Lumen the URIs it should respond to
| and give it the Closure to call when that URI is requested.
|
*/

$router->group(['prefix' => 'api'], function () use ($router) {
    $router->get('/', function () use ($router) {
        return "API";
    });

    $router->post('/signin','UserController@signin');
    $router->post('/signup','UserController@signup');

    $router->group(['middleware' => 'auth'], function () use ($router) {
        $router->get('/details', 'UserController@details');
    });
});

In order to group routs we have to use:

$router->group(['middleware' => 'auth'], function () use ($router) {
        $router->get('/details', 'UserController@details');
});