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

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

    点赞 打赏 评论
  • dongzhu0327 2016-01-14 09:09

    Tested and absolutely working. I added this in my dir/app/Http/routes.php file.

    $app->get('/', function() use ($app) {
    return $app->welcome(); });

    $app->get('/home', function() use ($app) { return "Welcome to Homepage"; });

    点赞 打赏 评论

相关推荐 更多相似问题