我正在使用Laravel 5.2 . 我想问一个问题 . 当我们已经登录时,如何在laravel上更改重定向登录表单?我正在使用来自Php artisan make的auth:auth . 这就是我的意思:
-
我已登录并重定向到localhost:8000 / admin
-
然后我再次打开localhost:8000 / login . 它将重定向到"/"或localhost:8000 /
-
我的问题是如何将"/"更改为"/admin"?所以,即使我已经登录,当我想打开localhost:8000 / login时,我不希望它重定向到"/"但是要"/admin"或localhost:8000 / admin
我认为它在AuthController上,所以这里是我的AuthController:
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
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 $redirectPath = '/admin';
protected $redirectTo = '/admin';
protected $redirectAfterLogout = '/login';
/**
* Create a new authentication controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware($this->guestMiddleware(), ['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|min:6|confirmed',
]);
}
/**
* 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']),
]);
}
}
当我搜索什么是Route :: auth()时,我发现了这个:
$this->get('login', 'Auth\AuthController@showLoginForm');
$this->post('login', 'Auth\AuthController@login');
$this->get('logout', 'Auth\AuthController@logout');
// Registration Routes...
$this->get('register', 'Auth\AuthController@showRegistrationForm');
$this->post('register', 'Auth\AuthController@register');
// Password Reset Routes...
$this->get('password/reset/{token?}', 'Auth\PasswordController@showResetForm');
$this->post('password/email', 'Auth\PasswordController@sendResetLinkEmail');
$this->post('password/reset', 'Auth\PasswordController@reset');
所以我认为问题在于'Auth \ AuthController @ showLoginForm' . 然后在AuthController我找不到
public function showLoginForm {}
所以,如果我想用这个功能做点什么,我该怎么办?看起来如果我已经登录然后重定向到/ admin else登录(或重定向到/登录)
这是我的路线:
Route::get('/', function () {
return view('welcome');
});
//admin
Route::get('admin', function() {
return view('admin.index');
});
//login
Route::auth();
Route::get('/home', 'HomeController@index');
感谢您的回复 .
1 回答
您可以像这样编辑
App\Http\Middleware\RedirectIfAuthenticated
中间件:或者你可能有专门的中间件 .
在您的控制台
php artisan make:middleware RedirectIfAdmin
并编辑handle
方法,如上所述,然后在App\Http\Kernel
中注册该中间件,如下所示:并将该中间件附加到控制器构造器: