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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46349867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:41:13  来源:igfitidea点击:

Route Group in Lumen error Call to undefined method Laravel\Lumen\Application::group()

phplaravelroutinglumenlumen-5.2

提问by Shakti Phartiyal

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

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

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

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

路由文件 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');
});

on making a call to http://THE_URL/

关于拨打http://THE_URL/

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

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

ERROR

错误

How do I add route group with a middleware ?

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

回答by zedomel

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

实际上正如@Aine 在 Lumen 5.5+ 中所说的,你应该改变:

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

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

to

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

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

Application does not have group()method anymore.

应用程序没有group()方法了。

thanks

谢谢

回答by Aine

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

我刚刚遇到了同样的问题,将 Lumen 从 5.4 升级到 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'; });

更新路由文件

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

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

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

对我来说,这意味着在路由中使用 $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();

Hope this helps.

希望这可以帮助。

回答by Shakti Phartiyal

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