【问题标题】:Laravel 5 Function () not found未找到 Laravel 5 函数()
【发布时间】:2015-04-14 02:24:04
【问题描述】:

我正在整理一个网站,该网站有一个受保护的部分,用户必须登录才能访问。我在 Laravel 4 中做到了这一点,没有发生太多事件。但是,对于我的生活,我无法弄清楚为什么我不能让它在 Laravel 5(L5) 中工作。

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

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

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

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

哎呀,好像出了点问题。

Route.php 第 150 行中的反射异常:

函数()不存在

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

【问题讨论】:

    标签: php laravel middleware laravel-routing laravel-5


    【解决方案1】:

    你忘记了uses 键:

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

    【讨论】:

    • 谢谢!我知道这一定是我刚刚忽略的东西。
    • /** 哦不! **/
    • 我已经为这个错误苦苦挣扎了 1 个小时。我写了user 而不是uses。谢谢:p
    【解决方案2】:

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

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

    但是如果你想添加中间件你需要写:

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

    【讨论】:

      【解决方案3】:

      如果您的视图不使用控制器而只想显示视图,您应该这样做:

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

      【讨论】:

        猜你喜欢
        • 2015-05-05
        • 2015-05-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-28
        • 2015-04-29
        • 2015-08-18
        • 2016-05-16
        • 2015-07-07
        相关资源
        最近更新 更多