我试图登录,但它一直将我重定向回主页,在laravel 5.2中我不会有这个问题。
在此先感谢,似乎没有关于此更改的教程。
web.php
Route::get('/dashboard', [
'uses' => 'UserController@getDashboard',
'as' => 'dashboard',
'middleware' => 'auth'
]);
我将其追溯到此。
kernal.php
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
];
Usercontroller.php
public function userLogin(Request $request)
{
$remember = $request->input['remember_me'];
if(Auth::attempt(['username'=> $request['username'], 'password' => $request['password']], $remember )){
return redirect()->route('dashboard');
}
return redirect()->back();
}
public function getDashboard()
{
$cookie = cookie('saw-dashboard', true, 15);
$users = User::all();
$user = new User();
return view('dashboard', array('user'=> Auth::user()), compact('users'))->withCookie($cookie);
}
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class Authenticate
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
* @return mixed
*/
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->guest()) {
if ($request->ajax() || $request->wantsJson()) {
return response('Unauthorized.', 401);
} else {
return redirect()->route('home');
}
}
return $next($request);
}
}
我检查了我的网络控制台,但是找不到302,不知道该做什么。
答案 0 :(得分:1)
像这样改变路线
Route::group(['middleware' => ['auth']], function() {
Route::get('/dashboard', 'UserController@getDashboard')->name('dashboard');
});
答案 1 :(得分:0)
尝试使用像这样的中间件:
<强>中间件( 'AUTH:API'); 强>