dongya8378 2015-10-02 11:06
浏览 119
已采纳

Lumen / Laravel - 自定义身份验证中间件错误

I've created authentication middleware for a lumen project. The authentication uses an external API, but unless I'm mistaken, that's not the issue here. The middleware correctly redirects me to the login page if I'm not authenticated, and correctly logs me in and saves it in the session when I put in good credentials. But it won't display the internal homepage as long as I'm using the middleware. The "handle" method in App\Http\Middleware\Authenticate.php seems to have no Response in $next($request) to return and I'm not sure why.

If my UserProvider implementation or AuthController is needed, let me know.

Error Message:

ReflectionException in Container.php line 554: Function () does not exist

in Container.php line 554
at ReflectionFunction->__construct('') in Container.php line 554
at Container->getCallReflector(null) in Container.php line 531
at Container->getMethodDependencies(null, array()) in Container.php line 500
at Container->call(null, array()) in Application.php line 1286
at Application->callActionOnArrayBasedRoute(array(true, array('middleware' => 'bankerAuth'), array())) in Application.php line 1255
at Application->Laravel\Lumen\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Authenticate.php line 44
at Authenticate->handle(object(Request), object(Closure))
at call_user_func_array(array(object(Authenticate), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Application.php line 1411
at Application->sendThroughPipeline(array('App\Http\Middleware\Authenticate'), object(Closure)) in Application.php line 1256
at Application->handleFoundRoute(array(true, array('middleware' => 'bankerAuth'), array())) in Application.php line 1179
at Application->Laravel\Lumen\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in ShareErrorsFromSession.php line 54
at ShareErrorsFromSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in StartSession.php line 62
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure))
at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Application.php line 1411
at Application->sendThroughPipeline(array('Illuminate\Cookie\Middleware\EncryptCookies', 'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse', 'Illuminate\Session\Middleware\StartSession', 'Illuminate\View\Middleware\ShareErrorsFromSession'), object(Closure)) in Application.php line 1185
at Application->dispatch(null) in Application.php line 1125
at Application->run() in index.php line 28

Relevant code:

.env

CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=database
AUTH_DRIVER=bankerAuth

app.php

$app->middleware([
    Illuminate\Cookie\Middleware\EncryptCookies::class,
    Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
    Illuminate\Session\Middleware\StartSession::class,
    Illuminate\View\Middleware\ShareErrorsFromSession::class,
]);

$app->routeMiddleware([
    'bankerAuth' => \App\Http\Middleware\Authenticate::class,
//    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class
]);

$app['auth']->extend('bankerAuth', function()
{
    return new App\Banker\BankerUserProvider;
});

routes.php

$app->get('/', ['middleware' => 'bankerAuth'], function () use ($app) {
    return 'Home page';
});


$app->get('/login', function () {

    return view('login');
});


$app->post('/login', ['uses' => 'AuthController@postLogin'], function() {

});

App\Http\Middleware\Authenticate.php

<?php


namespace App\Http\Middleware;


use Closure;
use Illuminate\Contracts\Auth\Guard;

class Authenticate
{
    /**
     * The Guard implementation.
     *
     * @var Guard
     */
    protected $auth;

    /**
     * Create a new filter instance.
     *
     * @param  Guard  $auth
     * @return void
     */
    public function __construct(Guard $auth)
    {
        $this->auth = $auth;
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ($this->auth->guest()) {
            session()->flash('url.intended', app('url')->full());
            return redirect()->to('/login', 302);
        }

        return $next($request);
    }
}
  • 写回答

2条回答 默认 最新

  • dssjxvbv918586 2015-10-02 11:53
    关注

    Your syntax in the route.php file is wrong.

    If you use a middleware in the route the function has to be in the array too:

    $app->get('/', ['middleware' => 'bankerAuth', function () use ($app) {
        return 'Home page';
    }]);
    

    http://laravel.com/docs/5.1/middleware#registering-middleware

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 MATLAB APP 制作出现问题
  • ¥15 wannier复现图像时berry曲率极值点与高对称点严重偏移
  • ¥15 利用决策森林为什么会出现这样·的问题(关键词-情感分析)
  • ¥15 DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI[/untitled30_war_e
  • ¥15 使用deepspeed训练,发现想要训练的参数没有梯度
  • ¥15 寻找一块做为智能割草机的驱动板(标签-stm32|关键词-m3)
  • ¥15 信息管理系统的查找和排序
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),怎么用读取的电磁传感器信号表示小车所在的位置
  • ¥15 如何解决y_true和y_predict数据类型不匹配的问题(相关搜索:机器学习)
  • ¥15 PB中矩阵文本型数据的总计问题。