2017-05-12 70 views
0

我在laravel中有一箇中間件的問題。我想製作一箇中間件,允許剛註冊的用戶登錄。'是'我知道我可以通過'php artisan make: auth',但我想製作自己的中間件。 如果我在URL裏輸入「/儀表盤」它重定向我中心什麼是真的不好 ,也適用於在Laravel中的Auth中間件5.4

簽署所以我寫了中間件:

<?php 
namespace App\Http\Middleware; 

use Closure; 
use Illuminate\Support\Facades\Auth; 

class AuthUser 
{ 
    /** 
    * Handle an incoming request. 
    * 
    * @param \Illuminate\Http\Request $request 
    * @param \Closure $next 
    * @return mixed 
    */ 
    public function handle($request, Closure $next) 
    { 
     if(Auth::guard()->guest()) { 
      return redirect()->route('home'); 
     } 
     return $next($request); 
    } 
} 

然後將它添加到內核的路線。 PHP:

'auth_user'=>\App\Http\Middleware\AuthUser::class   

這裏是我的路線:

Route::get('/', function() { 
    return view('welcome'); 
})->name('home'); 

Route::post('/signup',[ 
    'uses'=>'[email protected]', 
    'as'=>'signup' 
]); 
Route::post('/signin',[ 
    'uses' =>'[email protected]', 
    'as' =>'signin' 
]); 

Route::group(['middleware'=>['auth_user']],function(){ 
    Route::get('/dashboard',[ 
     'uses'  => '[email protected]', 
     'as'  => 'dashboard', 
    ]); 
}); 

我的UserController中:

<?php 

namespace App\Http\Controllers; 

use Illuminate\Http\Request; 
use App\User; 
use Illuminate\Support\Facades\Auth; 

class UserController extends Controller 
{ 
    public function getDashboard() 
    { 
     return view('dashboard'); 
    } 
    public function postSignUp(Request $request) 
    { 
     $this->validate($request,[ 
      'email'  => 'email|unique:users', 
      'first_name'=> 'required|max:128', 
      'password' => 'required|min:6' 
     ]); 
     $email=$request['email']; 
     $password=bcrypt($request['password']); 
     $first_name=$request['first_name']; 
     $surname=($request['surname']); 
     $user=new User(); 

     $user->email=$email; 
     $user->password=$password; 
     $user->first_name=$first_name; 
     $user->surname=$surname; 

     $user->save(); 
     Auth::login($user); 
     return redirect()->route('dashboard'); 
    } 
    public function postSignIn(Request $request) 
    { 
     $this->validate($request,[ 
      'email'  => 'required|email', 
      'password' => 'required|min:6' 
     ]); 

if(Auth::attempt(['email' => $request['email'], 'password'  
      =>$request['password']]))  
     { 

      return redirect()->route('dashboard'); 
     } 
      return redirect()->back(); 
    } 
} 
+0

在5.4你[不應該加上'web'中間件(http://stackoverflow.com/questions/36784253/ laravel-5-2-validation-error-not-in-blade)到'web.php'中的路由。 –

+0

我從路由中刪除它,但仍然無效 – OrestKhomitskyi

回答

1

在路由/ web.php定義路由時你並不需要有「網絡」的中間件。

它通過你的RoutingServiceProvider應用於路線/ web.php路由默認

Route::group(['middleware'=>['auth_user']],function(){ 
    Route::get('/dashboard',[ 
     'uses'  => '[email protected]', 
     'as'  => 'dashboard', 
    ]); 
}); 
+0

謝謝,現在如果我鍵入/儀表板我得到重定向到反向頁面,但如果我登錄它不重定向到儀表板 – OrestKhomitskyi

+0

也許問題出在中間件類 – OrestKhomitskyi

+0

您能否更新你的新路線文件? – fppz

相關問題