3
我無法找到我的拼圖。當我加載頁面時,鉻顯示:本地主機頁面不工作,本地主機重定向您太多次了。我想這是創建一個循環。我可能做錯了我無法得到的東西。這裏是我的代碼。Laravel重定向循環
//route
Route::get('admin/login', 'Auth\[email protected]');
Route::post('admin/login', 'Auth\[email protected]');
Route::get('admin/logout', 'Auth\[email protected]');
Route::group(['middleware' => 'auth'], function() {
Route::Auth();
Route::get('/admin', 'admin\[email protected]');
});
Route::get('/', 'guest\[email protected]');//access all
AuthController
<?php
namespace App\Http\Controllers\Auth;
use App\Models\Admin;
use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Illuminate\Http\Request;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\Registrar;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use Illuminate\Http\Response;
class AuthController extends Controller
{
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
protected $redirectTo = '/admin';
public function __construct() {
$this->middleware($this->guestMiddleware(), ['except' => 'logout']);
}
protected function postLogin(Request $request){
$email = $request['email'];
$pass = $request['password'];//Hash::make(Input::get('password'));
$user = Admin::where(['email' => $email])->first();
if($user && $user->password == $pass) {
Auth::login($user);
dd(Auth::user()->toArray());
return redirect()->intended('/admin');
}
else {
$error = "Invalid username or password";
return view("admin/login")->with("error", $error);
}
}
protected function getLogout() {
Auth::logout();
return redirect()->intended('admin/login');
}
}
模型
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable;
class Admin extends Model implements Authenticatable
{
use \Illuminate\Auth\Authenticatable;
protected $table = 'admins';
public $timestamps = false;
public $remember_token = null;
}
Authenticate.php
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class Authenticate
{
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->guest()) {
if ($request->ajax() || $request->wantsJson()) {
return response('Unauthorized.', 401);
} else {
return redirect()->guest('admin/login');
}
}
return $next($request);
}
}
配置/ auth.php
//---------------before-------------------
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
//----------------after editing--------------------------
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\Admin::class,
],
您的系統是否僅以用戶身份處理管理員?如果沒有,你將不得不創建一個自定義中間件來重定向,如果不是管理員。 –
有3個或更多類型的用戶我如何創建自定義中間件@HariHarker –
請按照此問題的答案中提到的步驟 - http://stackoverflow.com/questions/34614753/can-anyone-explain-laravel-5-2-多AUTH與 - 例如 –