laravel中间件几乎总是重定向

时间:2017-01-27 12:56:24

标签: php laravel redirect laravel-5 laravel-5.3

你们可以请,帮助我。我在Laravel 5.3本地运行的项目一切正常,我在服务器上传了我的项目,它也可以正常工作。然后我将我的项目上传到另一台服务器上 - 它几乎总是重定向到登录页面。这是逻辑 - 如果用户未经过身份验证,则会重定向到登录页面。如果用户已通过身份验证,则用户将可以访问内容。在我的“另一个”服务器上 - 当我转到主页面时,它会给出

page isn’t working
redirected you too many times.
Try clearing your cookies.
ERR_TOO_MANY_REDIRECTS

所以这不是Laravel的错误。当我进入登录页面时,它会加载,但是当我登录时,它会重定向到主页,并显示ERR_TOO_MANY_REDIRECTS错误。它总是以这种方式运作。

路由

Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');
Route::post('login', 'Auth\LoginController@login');
Route::post('logout', 'Auth\LoginController@logout')->name('logout');

登录控制器

class LoginController extends Controller
{

use AuthenticatesUsers;

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

/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('guest', ['except' => 'logout']);
}


/**
 * Handle a login request to the application.
 */

public function login(Request $request)
{
    $this->validateLogin($request);

    $user = $this->findOrCreateUser($request);

    if($user)
    {
        Auth::login($user);

        $user->incrementVisists();

        if($user->count_visits > 3)
        {
            return redirect('/campaigns');
        }else{
            return redirect('/');
        }

    }else{

        if ($this->hasTooManyLoginAttempts($request))
        {
            $this->fireLockoutEvent($request);
            return $this->sendLockoutResponse($request);
        }

        $this->incrementLoginAttempts($request);

        return $this->sendFailedLoginResponse($request);

    }

}

/**
 * Validate the user login request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return void
 */
protected function validateLogin(Request $request)
{
    $this->validate($request, [
        $this->username() => 'required',
        $this->password() => 'required'
    ]);
}

/**
 * Get the login username to be used by the controller.
 *
 * @return string
 */
public function username()
{
    return 'username';
}

/**
 * Get the login password to be used by the controller.
 *
 * @return string
 */
public function password()
{
    return 'password';
}

/**
 *  find or create user
 */
public function findOrCreateUser(Request $request)
{
    $auth = $this->getUserDataFromRemote($request->username, $request->password);

    if($auth == "OK")
    {
        if(User::where('name', $request->username)->count() < 1)
        {
            $user = User::create([
                'name' => $request->username,
                'email' => '',
                'role' => 1,
                'password' => bcrypt($request->password),
            ]);
        }else{
            $user = User::where('name', $request->username)->first();
        }

        return $user;
    }

    return false;
}

}

我发现它发生了导致RedirectIfAuthenticated中间件

class RedirectIfAuthenticated
{

public function handle($request, Closure $next, $guard = null)
{
    if (Auth::guard($guard)->check()) {
        //return redirect('/home');
        return redirect('/');
    }

    return $next($request);
}
}

但不知道我应该怎么解决它。即使问题出现在这个中间件中,为什么它在本地和我的第一台服务器上工作?

主要路线

Route::get('/', 'WelcomesController@index')->name('mainPage');

WelcomesController中的索引方法

public function index()
{
    if(\Auth::check() && \Auth::user()->count_visits > 3)
    {
        return redirect()->action('CampaignsController@index');
    }

    $welcome = Welcome::first();;

    //activate the first menu item
    Menu::get('mainMenu')->find('chooseAWebsite')->active();
    foreach(Menu::get('mainMenu')->find('chooseAWebsite')->children() as $item){
        $item->isActive=false;
    }


    return view('welcome.index', compact('welcome'));
}

campaignsController

 public function __construct() {
   $this->middleware('auth');
   $this->middleware('menu');
}

public function index() {

    $campaigns = Campaign::where('user_id',Auth::id())
        ->paginate(25);

    $layouts = \App\Layout::pluck('url', 'id');

    return view('campaigns.index', compact('campaigns', 'layouts'));
}

0 个答案:

没有答案