2016-11-03 103 views
0

我使用Laravel 5.2。我想問一個問題。當我們已經登錄後,如何在laravel上更改重定向登錄表單?我使用Php artisan make auth驗證。這裏是我的意思是:Laravel驗證登錄窗體重定向時已經登錄

  • 我已經登錄並重定向到本地主機:8000 /管理員
  • 然後我再次打開本地主機:8000 /登錄。它會重定向到「/」或本地主機:8000/
  • 我的問題是如何改變這種「/」到「/ admin」的?所以,即使我已經登錄,當我想打開本地主機:8000 /登錄,我不希望它重定向到「/」,而是「/ admin」的或本地主機:8000 /管理員

我認爲其對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']), 
     ]); 
    } 
} 
當我搜索的是

路線:: AUTH(),我發現這一點:

$this->get('login', 'Auth\[email protected]'); 
$this->post('login', 'Auth\[email protected]'); 
$this->get('logout', 'Auth\[email protected]'); 

// Registration Routes... 
$this->get('register', 'Auth\[email protected]'); 
$this->post('register', 'Auth\[email protected]'); 

// Password Reset Routes... 
$this->get('password/reset/{token?}', 'Auth\[email protected]'); 
$this->post('password/email', 'Auth\[email protected]'); 
$this->post('password/reset', 'Auth\[email protected]'); 

所以我覺得這個問題是 '驗證\ AuthController @ showLoginForm'。然後在AuthController我找不到

public function showLoginForm {} 

所以如果我想用這個功能做什麼,我必須做什麼?它的樣子,如果我已經登錄然後重定向到/管理員做別的登錄(或重定向到/登錄)

,這裏是我的路線:

Route::get('/', function() { 
    return view('welcome'); 
}); 

//admin 
Route::get('admin', function() { 
    return view('admin.index'); 
}); 

//login 
Route::auth(); 
Route::get('/home', '[email protected]'); 

感謝您的答覆。

回答

1

您可以編輯App\Http\Middleware\RedirectIfAuthenticated中間件,像這樣:

public function handle($request, Closure $next, $guard = null) 
{ 
    if (Auth::guard($guard)->check()) { 
     return redirect('/admin'); 
    } 

    return $next($request); 
} 

或者你可能有一箇中間件專門爲。

在控制檯 php artisan make:middleware RedirectIfAdmin

和編輯handle方法像上面一個然後註冊中間件在App\Http\Kernel這樣的:

protected $routeMiddleware = [ 
    ... 
    'RedirectIfAdmin' => \App\Http\Middleware\RedirectIfAdmin::class, 
]; 

和中間件連接到控制器constuctor:

public function __construct() 
{ 
    $this->middleware('RedirectIfAdmin', ['except' => 'logout']); 
} 
+0

感謝非常太多了!我不知道我們可以在中間件上做到這一點..我是laravel上的新手,所以我不知道它..謝謝!你幫我很多! –

+0

歡迎您嘗試查看[documentation](https://laravel.com/docs/5.2/authentication) –