Laravel 5 函数()未找到

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

Laravel 5 Function () not found

phplaravelmiddlewarelaravel-routinglaravel-5

提问by joseph

I'm putting together a site which has a protected section where users must be logged in to access. I've done this in Laravel 4 without too much incident. However, for the life of me I cannot figure out why I can't get it to work in Laravel 5(L5).

我正在建立一个站点,该站点具有受保护的部分,用户必须登录才能访问该部分。我在 Laravel 4 中做到了这一点,没有发生太多意外。但是,对于我的一生,我无法弄清楚为什么我无法在 Laravel 5(L5)中使用它。

In L5 middleware was/were introduced. This changes the route file to:

在 L5 中引入了中间件。这会将路由文件更改为:

Route::get('foo/bar', ['middleware'=>'auth','FooController@index']);
Route::get('foo/bar/{id}', ['middleware'=>'auth','FooController@show']);

The route works fine as long as the middleware is not included.

只要不包含中间件,该路由就可以正常工作。

When the route is accessed with the middleware however the result is not so much fun.

然而,当使用中间件访问路由时,结果就没有那么有趣了。

Whoops, looks like something went wrong.

ReflectionException in Route.php line 150:

Function () does not exist

哎呀,看起来出事了。

Route.php 第 150 行中的 ReflectionException:

函数()不存在

Any insight, help, and/or assistance is very appreciated. I've done the Google circuit and couldn't find anything relevant to my current plight. Thanks in advance.

非常感谢任何见解、帮助和/或协助。我已经完成了谷歌电路,但找不到与我目前的困境相关的任何内容。提前致谢。

回答by manix

you forgot the useskey :

你忘记了uses钥匙:

Route::get('foo/bar/{id}', ['middleware'=>'auth', 'uses'=>'FooController@show']);

回答by Marcin Nabia?ek

If you add anything more than your controller method into your routes you need to add usesas key of the array for your controller, so for example if I don't haw any middleware it's enough to write:

如果您在路由中添加的不仅仅是控制器方法,则需要添加uses为控制器数组的键,例如,如果我没有任何中间件,那么编写就足够了:

Route::get('foo/bar', 'FooController@index');
Route::get('foo/bar/{id}', 'FooController@show');

However if you want to add middleware you need to write:

但是,如果要添加中间件,则需要编写:

Route::get('foo/bar', ['middleware'=>'auth','uses' => 'FooController@index']);
Route::get('foo/bar/{id}', ['middleware'=>'auth','uses' => 'FooController@show']);

回答by paulalexandru

In case you don't use a controller for your view and you just want to display the view, you should do this:

如果您不为视图使用控制器而只想显示视图,则应执行以下操作:

Route::get('foo/bar', ['middleware' => 'auth', function () {
    return View::make('path.to.your.page');
}]);