Laravel重定向循环

时间:2016-10-03 13:45:07

标签: php laravel laravel-5.2

我无法找到我的谜题。当我加载页面时,chrome说:localhost页面不起作用,localhost重定向了你太多次了。我想它正在创建一个循环。我可能做错了,我无法得到。这是我的代码。

   //route
 Route::get('admin/login', 'Auth\AuthController@getlogin');
    Route::post('admin/login', 'Auth\AuthController@postLogin');
    Route::get('admin/logout', 'Auth\AuthController@getLogout');

    Route::group(['middleware' => 'auth'], function () {
        Route::Auth();
        Route::get('/admin', 'admin\HomeController@index');
    });


    Route::get('/', 'guest\HomeController@index');//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,
    ],

1 个答案:

答案 0 :(得分:2)

您的Auth路由不应该应用auth中间件。所以:

Route::Auth();    

Route::group(['middleware' => 'auth'], function () {
    Route::get('/admin', 'admin\HomeController@index');
});