流明中的路由组错误调用未定义的方法Laravel \ Lumen \ Application :: group()

时间:2017-09-21 17:27:23

标签: php laravel routing lumen lumen-5.2

我已经在laravel / lumen中声明了一个路由组,如下所示:

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

路由文件web.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.
|
*/

$app = app();

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


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

打电话给http://THE_URL/

我收到错误Call to undefined method Laravel\Lumen\Application::group()

ERROR

如何使用中间件添加路由组?

3 个答案:

答案 0 :(得分:5)

实际上,如@Aine在流明5.5及更高版本中所说,您应该更改:

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

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

应用程序不再具有group()方法。

谢谢

答案 1 :(得分:3)

我遇到了同样的问题,将流明从5.4升级到5.5

all中,它指出:

  

更新路线文件

     

更新bootstrap / app.php文件后,   您应该更新您的route / web.php文件以使用$ router   变量而不是$ app变量:

     

$ router-> get('/ hello',function(){       返回“ Hello World”; });

对我来说,这意味着要进行大量更新,而路由中曾经使用$ app。

$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();

希望这会有所帮助。

答案 2 :(得分:0)

找到解决问题的方法:

<?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');
    });
});

为了分组我们必须使用的路径:

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