2015-01-15 113 views
0

我正在使用Larave 4.2,並且一切正常。如果我輸入正確的憑證,我將被帶到正確的URL(具有auth過濾器的URL)。但是,我目前遇到的問題是,當輸入的一個字段不正確,用戶提交時會顯示白屏。WSOD驗證失敗結果

我期待當然用戶將被重定向回到帶有輸入的登錄頁面並顯示錯誤。

我檢查了過濾器,並且很確定它仍然是Laravel的產品,並且沒有任何改變。

我的路線

<?php 

Route::get('login', function() 
{ 
    // just a shortcut to redirec to /login into /cms/login : prevents redirect LOOP 
    return Redirect::route('cms.login'); 
}); 

Route::group(array('prefix' => 'cms'), function() 
{ 
    Route::get('/', function() 
    { 
     if (Auth::guest()) 
     { 
      return Redirect::route('cms.login'); 
     } 
     else 
     { 
      return Redirect::route('cms.home'); 
     } 
    }); 

    Route::get('login', array(
     'as' => 'cms.login', 
     'uses' => '[email protected]' 
    )); 

    Route::post('login', array(
     'as' => 'cms.postLogin', 
     'uses' => '[email protected]' 
    )); 

    Route::get('logout', array(
     'as' => 'cms.logout', 
     'uses' => '[email protected]' 
    )); 

    Route::group(array('before' => 'auth'), function() 
    { 
     Route::get('home', array(
      'as' => 'cms.home', 
      'uses' => '[email protected]' 
     )); 

     Route::get('my-account', array(
      'as' => 'cms.myaccount', 
      'uses' => '[email protected]' 
     )); 

     Route::get('my-account/edit', array(
      'as' => 'cms.edit-myaccount', 
      'uses' => '[email protected]' 
     )); 

     Route::resource('accounts', 'AccountsController'); 
     Route::resource('products', 'ProductsController'); 
     Route::resource('news', 'NewsController'); 
     Route::resource('settings', 'SettingsController'); 
     Route::resource('homepage-sliders', 'HomepageSlidersController'); 
     Route::resource('testimonials', 'TestimonialsController'); 
     Route::resource('effects', 'EffectsController'); 
    }); 
}); 

用戶模型

<?php 

use Illuminate\Auth\UserTrait; 
use Illuminate\Auth\UserInterface; 
use Illuminate\Auth\Reminders\RemindableTrait; 
use Illuminate\Auth\Reminders\RemindableInterface; 

class User extends Eloquent implements UserInterface, RemindableInterface { 

    use UserTrait, RemindableTrait; 

    /** 
    * The database table used by the model. 
    * 
    * @var string 
    */ 
    protected $table = 'users'; 

    /** 
    * Fillable array 
    * 
    */ 
    protected $fillable = array('email', 'password', 'username', 'position', 'mobile'); 

    /** 
    * The attributes excluded from the model's JSON form. 
    * 
    * @var array 
    */ 
    protected $hidden = array('password', 'remember_token'); 

    /** 
    * Sets the Validation Rules when Logging In 
    * 
    * @var array 
    */ 
    public static $loginRules = array(
     'email' => 'required|email', 
     'password' => 'required|alpha_dash|min:6' 
    ); 

    /** 
    * Sets the Validation Rules creating a User 
    * 
    * @var array 
    */ 
    public static $rules = array(
     'email'     => 'required|email|unique:users', 
     'username'    => 'required|min:2|unique:users', 
     'position'    => 'required|', 
     'mobile-number'   => 'required|numeric|digits:11', 
     'password'    => 'required|alpha_dash|min:6|confirmed', 
     'password_confirmation' => 'required|alpha_dash|min:6' 
    ); 

    /** 
    * Sets the Validation Rules updating a User 
    * 
    * @var array 
    */ 
    public static $updateRules = array(
     'username'    => 'required|min:2', 
     'password'    => 'required|alpha_dash|min:6|confirmed', 
     'password_confirmation' => 'required|alpha_dash|min:6' 
    ); 

    /** 
    * Defines many-to-many relationship with Module 
    * 
    */ 
    public function permissions() 
    { 
     return $this->belongsToMany('Module', 'permissions')->withPivot('add','edit', 'view','delete'); 
    } 

    /** 
    * Get the unique identifier for the user. 
    * 
    * @return mixed 
    */ 
    public function getAuthIdentifier() 
    { 
     return $this->getKey(); 
    } 

    /** 
    * Get the password for the user. 
    * 
    * @return string 
    */ 
    public function getAuthPassword() 
    { 
     return $this->password; 
    } 

    /** 
    * Get the e-mail address where password reminders are sent. 
    * 
    * @return string 
    */ 
    public function getReminderEmail() 
    { 
     return $this->email; 
    } 

    /** 
    * Gets the Remember Token 
    * 
    * @return string $this->remember_token 
    */ 
    public function getRememberToken() 
    { 
     return $this->remember_token; 
    } 

    /** 
    * Set the Remember Token 
    * 
    * @param string $value 
    */ 
    public function setRememberToken($value) 
    { 
     $this->remember_token = $value; 
    } 

    /** 
    * Get the Remember Token name 
    * 
    * @return string 'remember_token' 
    */ 
    public function getRememberTokenName() 
    { 
     return 'remember_token'; 
    } 

    /** 
    * Get the password and Hash it before saving to the database. 
    * 
    * @param  string $value 
    */ 
    public function setPasswordAttribute($value) 
    { 
     $this->attributes['password'] = Hash::make($value); 
    } 

    /** 
    * Checks if Guest User input invalid credentials 
    * 
    * @param  array  $credentials 
    * @return object $validation 
    */ 
    public static function loginIsInvalid($credentials) 
    { 
     $validation = Validator::make($credentials, self::$loginRules); 

     if ($validation->fails()) 
     { 
      return $validation; 
     } 
    } 

我CMSController

<?php 

class CMSController extends BaseController { 

    /** 
    * Display the login page. 
    * GET /cms 
    * 
    * @return Response 
    */ 
    public function login() 
    { 
     return View::make('cms.login'); 
    } 

    /** 
    * Accepts the post request for login 
    * of user in CMS 
    * 
    */ 
    public function userLogin() 
    { 
     $user_credentials['email'] = Input::get('email'); 
     $user_credentials['password'] = Input::get('password'); 

     //sets the remember_me variable 
     if (Input::has('remember')) 
     { 
      $remember_me = true; 
     } 
     else 
     { 
      $remember_me = false; 
     } 

     if ($errors = User::loginIsInvalid($user_credentials)) 
     { 
      return Redirect::route('cms.login')->withInput()->withErrors($errors); 
     } 

     if (Auth::attempt(array(
      'email' => $user_credentials['email'], 
      'password' => $user_credentials['password']), $remember_me)) 
     { 
      return Redirect::route('cms.home'); 
     } 
    } 

    /** 
    * Accepts the post request for logout 
    * of user in CMS 
    * 
    */ 
    public function userLogout() 
    { 
     Session::clear(); 
     Auth::logout(); 

     return Redirect::route('cms.login'); 
    } 

    /** 
    * Directs user to home page 
    * 
    */ 
    public function home() 
    { 
     return View::make('cms.home'); 
    } 

} 

回答

2

目前在你的代碼沒有什麼Auth::attempt()後 - 因此,如果驗證失敗 - 它沒有去哪兒。

只需添加Auth::attempt()後返回,使其工作

if (Auth::attempt(array(
     'email' => $user_credentials['email'], 
     'password' => $user_credentials['password']), $remember_me)) 
    { 
     return Redirect::route('cms.home'); 
    } 

    return Redirect::route('cms.login')->withInput()->withErrors($errors);