Laravel - 太多重定向问题

时间:2016-07-26 18:01:48

标签: laravel laravel-5.2 laravel-routing laravel-middleware

我参与某个项目,该网站有两种类型的用户管理员用户。我为管理员制作了中间件中间件工作正常,但当我尝试登录时出现问题:

  

重定向太多

我的路线

<?php

Route::get('/', [
    'uses' => 'UserController@getIndex',
    'as' => 'welcome'
]);
Route::get('/{form?}', [
    'uses' => 'UserController@getIndex',
    'as' => 'index'
]);
Route::post('/exsist',[
    'uses' => 'UserController@postLogin',
    'as' => 'login'
]);
Route::group(['middleware' => 'auth'],function(){
    Route::group(['prefix' => '/admin','middleware' => 'admin'],function(){
        Route::group(['prefix' => '/my'],function(){
            Route::get('/home', [
                'uses' => 'AdminController@getHome',
                'as' => 'admin.home'
            ]);
        });
    });
    Route::group(['prefix' => '/'],function(){
        Route::get('/home', [
            'uses' => 'UserController@getHome',
            'as' => 'home'
        ]);
    });
});

和我的控制器

<?php

namespace App\Http\Controllers;

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

use App\Http\Requests;
use App\User;
class UserController extends Controller
{
        //main page
    public function getIndex($form = ''){
            if(Auth::check()){
                if(Auth::user()->isAdmin()){
                    return redirect()->route('admin.home');
                }else{
                    return redirect()->route('home');
                }
            }
            return view('welcome', ['form' => $form]);
        }
        //main login (index page)
        public function postLogin(Request $request){
            //validation
            $this->validate($request,[
                'email' => 'required|email',
                'pass' => 'required'
            ]);
            //authintecating user 
            if(Auth::attempt(['email'=>$request['email'], 'password'=>$request['pass']])){
                if(Auth::user()->isAdmin()){
                    return redirect()->route('admin.home')->with(['success'=>'You have successfuly loged in as Admin!']);
                }else{
                    return redirect()->route('home')->with(['success'=>'You have successfuly loged in as User!']);
                }
            }
            return redirect()->route('index')->with(['fail'=>'Wrong Email or Password!']);
        }
        public function getHome(){
            return view('home');
        }
}

为什么浏览器会给我一个错误?

1 个答案:

答案 0 :(得分:2)

尝试更改:

Route::get('/{form?}', [
'uses' => 'UserController@getIndex',
'as' => 'index'
]);

致:

Route::get('/form/{form?}', [
'uses' => 'UserController@getIndex',
'as' => 'index'
]);

我相信当你去&#39; /&#39;路线,&#39; / {form?}&#39;路线正在执行。