我在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();
}
}
在5.4你[不應該加上'web'中間件(http://stackoverflow.com/questions/36784253/ laravel-5-2-validation-error-not-in-blade)到'web.php'中的路由。 –
我從路由中刪除它,但仍然無效 – OrestKhomitskyi