我为流明项目创建了身份验证中间件。身份验证使用外部API,但除非我弄错了,这不是问题所在。如果我没有经过身份验证,中间件会正确地将我重定向到登录页面,并在我输入正确的凭据时正确地登录并将其保存在会话中。但只要我使用中间件,它就不会显示内部主页。 App \ Http \ Middleware \ Authenticate.php中的“handle”方法似乎在$ next($ request)中没有响应返回,我不知道为什么。
如果需要我的UserProvider实现或AuthController,请告诉我。
错误消息:
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=database
AUTH_DRIVER=bankerAuth
相关代码:
.ENV
$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;
});
app.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() {
});
routes.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);
}
}
应用\ HTTP \中间件\ Authenticate.php
DbConnection
答案 0 :(得分:2)
您在route.php文件中的语法错误。
如果在路径中使用中间件,则函数也必须在数组中:
$app->get('/', ['middleware' => 'bankerAuth', function () use ($app) {
return 'Home page';
}]);
http://laravel.com/docs/5.1/middleware#registering-middleware
答案 1 :(得分:-2)
经过测试并绝对正常工作。我在我的dir / app / Http / routes.php文件中添加了这个。
$app->get('/', function() use ($app) {
return $app->welcome();
});
$app->get('/home', function() use ($app) {
return "Welcome to Homepage";
});