laravel 在 null 上调用成员函数 middleware()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43644571/
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 15:51:47  来源:igfitidea点击:

Call to a member function middleware() on null

laravelroutingmiddlewarelaravel-5.4

提问by ramzi trabelsi

I use the following middleware in routing Laravel:

我在路由 Laravel 时使用以下中间件:

Route::group(['prefix' => 'api/'], function() {
    Route::resource('admin', 'adminController')->middleware('auth');
    Route::resource('profile', 'profileController')->middleware('role');
}); 

I get this error when i call 'admin' or 'profile' path in URL enter image description here

当我在 URL 中调用“admin”或“profile”路径时出现此错误 在此处输入图片说明

回答by lagbox

It is because Route::resource()does not return anything. Its void. It doesn't return an object.

这是因为Route::resource()不返回任何东西。其空。它不返回对象。

Laravel 5.4 - Illuminate\Routing\Router@resource

Laravel 5.4 - Illuminate\Routing\Router@resource

In Laravel 5.5 (in development), Route::resource()will be returning an object for fluently adding options to.

在 Laravel 5.5(开发中)中,Route::resource()将返回一个用于流畅地添加选项的对象。

回答by Daniel Masih

Use this

用这个

Route::prefix("/dashboard")->middleware(['first','second'])->group(function(){
});

回答by Jason

Most likely your resource controller is not resolving to an actual controller. Some things to check

很可能您的资源控制器没有解析为实际控制器。需要检查的一些事项

  • Check you actually have an adminController, and that the class name is in the correct case
  • Check that your controller is in the default namespace and, if not, change the controller namespace, or add a namespace attribute to your route
  • Check that your controller is not causing an exception on start that is being ignored resulting in you having a null controller.
  • 检查您实际上有一个 adminController,并且类名的大小写正确
  • 检查您的控制器是否在默认命名空间中,如果不是,请更改控制器命名空间,或向您的路由添加命名空间属性
  • 检查您的控制器是否在启动时不会导致被忽略的异常,从而导致您拥有一个空控制器。

回答by MrMedicine

Simply reverse the order:

只需颠倒顺序:

 Route::middleware('scope:clock-in')->resource('clock', 'ClockController');

As lagbox stated:

正如lagbox所说:

Route::resource()does not return anything.

Route::resource()不返回任何东西。

However middleware does.

但是中间件可以。