[英]Route Group in Lumen error Call to undefined method Laravel\Lumen\Application::group()
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()
How do I add route group with a middleware ? 如何使用中间件添加路由组?
Actually as @Aine said in Lumen 5.5+ you should change: 实际上,@ Aine在Lumen 5.5+中说你应该改变:
LumenPassport::routes($this->app);
to 至
LumenPassport::routes($this->app->router);
Application does not have group()
method anymore. 应用程序不再具有
group()
方法。
thanks 谢谢
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:
更新bootstrap / app.php文件后,您应该更新routes / web.php文件以使用$ router变量而不是$ app变量:
$router->get('/hello', function () { return 'Hello World'; });
$ 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. 希望这可以帮助。
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');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.