1
是否可以將用戶重定向到/登錄後?當我這樣做時,有,您沒有通過身份驗證,但登錄或註冊按鈕不起作用,因爲您已通過身份驗證。當我重定向到/ home時,它運行良好。它說,你是認證,一切正常。它仍然是相同的文件home.blade.php,只是url是不同的...(sudoman281.cz,sudoman281.cz/home)。謝謝。登錄或註冊後重定向到基本頁面Laravel
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()) {
return response('Unauthorized.', 401);
} else {
return redirect()->guest('login');
}
}
return $next($request);
}
}
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('home');
}
}
class AuthController extends Controller
{
/*
|--------------------------------------------------------------------------
| Registration & Login Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users, as well as the
| authentication of existing users. By default, this controller uses
| a simple trait to add these behaviors. Why don't you explore it?
|
*/
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
/**
* Where to redirect users after login/registration.
*
* @var string
*/
protected $redirectTo = '/';
/**
* Create a new authentication controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest', ['except' => 'logout']);
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
}
class RedirectIfAuthenticated
{
/**
* 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)->check()) {
return redirect('/');
}
return $next($request);
}
}
Route::get('/', function() {
return view('home');
});
Route::get('login', function() {
return view('auth.login');
});
Route::get('/register', function() {
return view('auth.register');
});
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| This route group applies the "web" middleware group to every route
| it contains. The "web" middleware group is defined in your HTTP
| kernel and includes session state, CSRF protection, and more.
|
*/
Route::group(['middleware' => ['web']], function() {
//
});
Route::group(['middleware' => 'web'], function() {
Route::auth();
Route::get('/', '[email protected]');
});
您已經保護了$ redirectTo ='/';將用戶重定向到'/',所以不確定你的問題是什麼。 –
嘗試清除您的路線緩存:php artisan路線:清除 如果它保持失敗,請將我們的路線發佈給我們(php artisan route:list)。它可能是緩存或拼寫錯誤。 – subzeta
爲什麼你有兩條路線Route :: get('/')?導致HomeController的第二個將不起作用。 – naneri