2017-06-12 44 views
0

LoginController將重定向循環返回到登錄路由。Auth :: routes()重定向循環

的LoginController:

<?php 

namespace App\Http\Controllers\Auth; 

use App\Http\Controllers\Controller; 
use Illuminate\Foundation\Auth\AuthenticatesUsers; 
use Illuminate\Support\Facades\Auth; 

class LoginController extends Controller 
{ 
    /* 
    |-------------------------------------------------------------------------- 
    | Login Controller 
    |-------------------------------------------------------------------------- 
    | 
    | This controller handles authenticating users for the application and 
    | redirecting them to your home screen. The controller uses a trait 
    | to conveniently provide its functionality to your applications. 
    | 
    */ 

    use AuthenticatesUsers; 

    /** 
    * Where to redirect users after login. 
    * 
    * @var string 
    */ 
    protected $redirectTo = '/register'; 

    /** 
    * Create a new controller instance. 
    * 
    */ 
    public function __construct() 
    { 
     $this->middleware('guest')->except('logout'); 
    } 

    public function username() 
    { 
     return 'username'; 
    } 

} 

路線/ web.php:

<?php 

/* 
|-------------------------------------------------------------------------- 
| Web Routes 
|-------------------------------------------------------------------------- 
| 
| Here is where you can register web routes for your application. These 
| routes are loaded by the RouteServiceProvider within a group which 
| contains the "web" middleware group. Now create something great! 
| 
*/ 

Auth::routes(); 

Route::get('/home', '[email protected]')->name('home'); 

// Enums Route // 

Route::get('/genderEnum', '[email protected]'); 

Route::get('/modelTypeEnum', '[email protected]'); 

Route::get('/classCodeEnum', '[email protected]'); 

Route::get('/statusEnum', '[email protected]'); 

Route::get('/titleEnum', '[email protected]'); 

// test routes // 

Route::get('/test', '[email protected]'); 

RouteServiceProvider:

<?php 

namespace App\Providers; 

use Illuminate\Support\Facades\Route; 
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider; 

class RouteServiceProvider extends ServiceProvider 
{ 
    /** 
    * This namespace is applied to your controller routes. 
    * 
    * In addition, it is set as the URL generator's root namespace. 
    * 
    * @var string 
    */ 
    protected $namespace = 'App\Http\Controllers'; 

    /** 
    * Define your route model bindings, pattern filters, etc. 
    * 
    * @return void 
    */ 
    public function boot() 
    { 
     // 

     parent::boot(); 
    } 

    /** 
    * Define the routes for the application. 
    * 
    * @return void 
    */ 
    public function map() 
    { 
     $this->mapApiRoutes(); 

     $this->mapWebRoutes(); 

     // 
    } 

    /** 
    * Define the "web" routes for the application. 
    * 
    * These routes all receive session state, CSRF protection, etc. 
    * 
    * @return void 
    */ 
    protected function mapWebRoutes() 
    { 

//  Route::middleware('web') 
//    ->namespace($this->namespace) 
//    ->group(base_path('routes/web.php')); 

     Route::group([ 
      'middleware' => ['auth', 'web', 'guest'], 
      'namespace' => $this->namespace 
     ],function(){ 
      require base_path('routes/web.php'); 
     }); 

//  Route::group(array(
//   'middleware' => ['web', 'auth'], 
//   'namespace' => $this->namespace, 
//  ), function ($router) { 
//   require base_path('routes/web.php'); 
//  }); 
    } 

    /** 
    * Define the "api" routes for the application. 
    * 
    * These routes are typically stateless. 
    * 
    * @return void 
    */ 
    protected function mapApiRoutes() 
    { 
     Route::prefix('api') 
      ->middleware('api') 
      ->namespace($this->namespace) 
      ->group(base_path('routes/api.php')); 
    } 
} 

我認爲這個問題與我RouteServiceProvider做。我嘗試授權用戶,當我點擊其他auth路由時,我被重定向回登錄視圖,然後繼續進入302重定向循環到登錄GET路由。

回答

1

您不應該使用「auth」中間件使用Auth路由。試着改變你的服務提供商:

Route::group([ 
    'middleware' => ['web', 'guest'], 
    'namespace' => $this->namespace 
],function(){ 
    require base_path('routes/web.php'); 
}); 

和路線/ web.php到:

<?php 

/* 
|-------------------------------------------------------------------------- 
| Web Routes 
|-------------------------------------------------------------------------- 
| 
| Here is where you can register web routes for your application. These 
| routes are loaded by the RouteServiceProvider within a group which 
| contains the "web" middleware group. Now create something great! 
| 
*/ 

Auth::routes(); 

Route::get('/home', '[email protected]')->name('home'); 

Route::group(['middleware' => ['auth']], function() { 

    // Enums Route // 

    Route::get('/genderEnum', '[email protected]'); 

    Route::get('/modelTypeEnum', '[email protected]'); 

    Route::get('/classCodeEnum', '[email protected]'); 

    Route::get('/statusEnum', '[email protected]'); 

    Route::get('/titleEnum', '[email protected]'); 

    // test routes // 

    Route::get('/test', '[email protected]'); 
}); 
+0

我在我的用戶模型中犯了一個錯誤,因爲我創建了我自己的基礎模型。我在其中擴展強制我實現Auth \ Authenticatable和Auth \ CanResetPassword以使用auth :: routes()。我在實現一些記憶標記函數和getAuthIdentifierName和getAuthIdentifier方法時犯了一個錯誤,這導致了路由問題。也是我在使用Auth :: user()方法時總是得到空用戶的原因。然而,在確定你的答案後,我正確地使用我的枚舉,以及如何使用其他控制器。謝謝。 –

0

我改變了我的RouteServiceProvider回到這個

RouteServiceProvider:

<?php 

namespace App\Providers; 

use Illuminate\Support\Facades\Route; 
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider; 

class RouteServiceProvider extends ServiceProvider 
{ 
    /** 
    * This namespace is applied to your controller routes. 
    * 
    * In addition, it is set as the URL generator's root namespace. 
    * 
    * @var string 
    */ 
    protected $namespace = 'App\Http\Controllers'; 

    /** 
    * Define your route model bindings, pattern filters, etc. 
    * 
    * @return void 
    */ 
    public function boot() 
    { 
     // 

     parent::boot(); 
    } 

    /** 
    * Define the routes for the application. 
    * 
    * @return void 
    */ 
    public function map() 
    { 
     $this->mapApiRoutes(); 

     $this->mapWebRoutes(); 

     // 
    } 

    /** 
    * Define the "web" routes for the application. 
    * 
    * These routes all receive session state, CSRF protection, etc. 
    * 
    * @return void 
    */ 
    protected function mapWebRoutes() 
    { 

     Route::middleware('web') 
      ->namespace($this->namespace) 
      ->group(base_path('routes/web.php')); 

    } 

    /** 
    * Define the "api" routes for the application. 
    * 
    * These routes are typically stateless. 
    * 
    * @return void 
    */ 
    protected function mapApiRoutes() 
    { 
     Route::prefix('api') 
      ->middleware('api') 
      ->namespace($this->namespace) 
      ->group(base_path('routes/api.php')); 
    } 
} 

用戶型號:

<?php 

namespace App; 


use Mockery\Exception; 
use Illuminate\Support\Facades\Hash; 

use Illuminate\Auth\Authenticatable; 
use Illuminate\Auth\Passwords\CanResetPassword; 

use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; 
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; 

use App\Model as Model; 

class User extends Model implements AuthenticatableContract, CanResetPasswordContract 
{ 

    use Authenticatable; 
    /** 
    * The attributes that are mass assignable. 
    * 
    * @var array 
    */ 
    protected $fillable = [ 
     'contact', 'username' 
    ]; 

    /** 
    * The column name of the "remember me" token. 
    * 
    * @var string 
    */ 
    protected $rememberTokenName = 'remember_token'; 

    /** 
    * The attributes that should be hidden for arrays. 
    * 
    * @var array 
    */ 
    protected $hidden = [ 
     'remember_token', 'active' 
    ]; 

    /** 
    * the attributes that should be guarded from Mass Assignment 
    * 
    * @var array 
    */ 
    protected $guarded = [ 
     'created_at', 'updated_at', 'password_hash' 
    ]; 

    /** 
    * Define table to be used with this model. It defaults and assumes table names will have an s added to the end. 
    *for instance App\User table by default would be users 
    */ 
    protected $table = "user"; 

    /** 
    * We have a non incrementing primary key 
    * 
    * @var bool 
    */ 
    public $incrementing = false; 

    /** 
    * relationships 
    */ 
    public function contact(){ 
//  return $this->hasOne(Contact::class, 'id', 'contact_id'); 
     return $this->hasOne(Contact::class); 
    } 

    /** 
    * User constructor. 
    * @param array $attributes 
    */ 
    public function __construct($attributes = array()) { 
     parent::__construct($attributes); // Eloquent 
     // Your construct code. 

     $this->active = 1; 

     return $this; 
    } 


    /** 
    * @param $password string 
    * set user password_hash 
    * @return $this 
    */ 
    public function setPassword($password){ 
     // TODO Password Validation 
     try{ 
      $this->isActive(); 
      $this->password_hash = Hash::make($password); 
      $this->save(); 
     } catch(\Exception $e) { 
      dump($e->getMessage()); 
     } 
     return $this; 
    } 


    /** 
    * Returns whether or not this use is active. 
    * 
    * @return bool 
    */ 
    public function isActive(){ 
     if($this->active) { 
      return true; 
     } else { 
      Throw new Exception('This user is not active. Therefore you cannot change the password', 409); 
     } 
    } 


    /** 
    * @return string 
    * 
    * getFullName 
    * returns concatenated first and last name of user. 
    */ 
    public function getFullName(){ 
     return $this->first_name . ' ' . $this->last_name; 
    } 


    /** 
    * Get the name of the unique identifier for the user. 
    * 
    * @return string 
    */ 
    public function getAuthIdentifierName(){ 
     return $this->getKeyName(); 

    } 

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

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

    /** 
    * Get the token value for the "remember me" session. 
    * 
    * @return string 
    */ 
    public function getRememberToken(){ 
     if (! empty($this->getRememberTokenName())) { 
      return $this->{$this->getRememberTokenName()}; 
     } 
    } 

    /** 
    * Set the token value for the "remember me" session. 
    * 
    * @param string $value 
    * @return void 
    */ 
    public function setRememberToken($value){ 
     if (! empty($this->getRememberTokenName())) { 
      $this->{$this->getRememberTokenName()} = $value; 
     } 
    } 

    /** 
    * Get the column name for the "remember me" token. 
    * 
    * @return string 
    */ 
    public function getRememberTokenName(){ 
     return $this->rememberTokenName; 
    } 

    /** 
    * Get the e-mail address where password reset links are sent. 
    * 
    * @return string 
    */ 
    public function getEmailForPasswordReset(){ 

    } 

    /** 
    * Send the password reset notification. 
    * 
    * @param string $token 
    * @return void 
    */ 
    public function sendPasswordResetNotification($token){ 

    } 

    public function validateAddress(){ 

    } 


}