中间件auth不使用Web Laravel 5.2

时间:2016-11-02 06:08:10

标签: php laravel authentication laravel-5.2

您好,我在laravel中遇到了一些身份验证问题。我必须使用两个中间件1.是web和2. auth。我正在使用Web中间件,以便我可以使用会话来显示Flash消息。并希望使用auth中间件对用户/管理员进行身份验证。但是我遇到了一些问题。

下面是我检查授权和重定向到各自路线的功能

  public function postLoginForm(){

    $email=Input::get('email');
    $password=Input::get('password');

    $data=[
        'email'=>$email,
        'password'=>$password
    ];
    $rules=[

        'email'=>'required',
        'password'=>'required'

    ];

    $validator=Validator::make($data,$rules);

    if($validator->fails()){

        Session::flash('fail', 'Oops Something went wrong!!');
        return redirect()->back()->withErrors($validator);

    }
    else{



        if(Auth::attempt($data)){




            $checkStatus=User::select('*')->where('email',$email)->first();

            Session::put('email',$checkStatus->email);
            Session::put('user_type',$checkStatus->user_type);




            if($checkStatus['user_type']=='4'){
                if($checkStatus['status']=='0'){
                    Session::flash('wait', 'Registration is not approved!!');

                    return "student";
                    return redirect()->back();
                }
                else{

                    return "student else";

                    return Redirect::route('get.student.dashBoard');

                }
            }
            else if($checkStatus['user_type']=='1'){


                return Redirect::route('get.admin.dashBoard');

            }
            else if($checkStatus['user_type']=='2'){


                return 'admin sir view';

                return Redirect::route('get.admin.dashBoard');
            }
            else if($checkStatus['user_type']=='3'){



                return 'admin other view';


                return Redirect::route('get.admin.dashBoard');
            }
            else{
                Session::flash('fail', 'Oops Something went wrong!!');
                return redirect()->back();
            }

        }
        else{
            Session::flash('fail', 'Login details not matched!!');
            return redirect()->back();
        }

    }
    return 'nothing works';

}

下面是我的管理路线

    Route::group(['middleware' => ['web']], function () {

Route::get('/login',
    ['as' => 'get.login.page',
        'uses' => 'LoginController@getLoginPage']);

Route::post('/login-done',
    ['as' => 'post.login.page',
        'uses' => 'LoginController@postLoginForm']);


Route::get('/register',
    ['as' => 'get.register.page',
        'uses' => 'LoginController@getRegisterPage']);

Route::post('/register',
    ['as' => 'post.register.form',
        'uses' => 'LoginController@postRegisterForm']);

Route::get('/forgot-password',
    ['as' => 'get.forgotPassword.form',
        'uses' => 'LoginController@getForgotPasswordForm']);



      Route::group(['middleware' => ['auth']], function () {


    Route::get('/admin-dashboard',
        ['as' => 'get.admin.dashBoard',
            'uses' => 'admin\PageController@getAdminDashboard']);


    Route::get('/all-achievements',
        ['as' => 'get.achievements',
            'uses' => 'admin\AchievementsController@getAchievementsList']);

    Route::get('/new-achievement',
        ['as' => 'get.add.achievement',
            'uses' => 'admin\AchievementsController@getAddAchievement']);

    Route::post('/add-achievement',
        ['as' => 'post.achievementsForm',
            'uses' => 'admin\AchievementsController@postAchievements']);


    Route::get('remove-achievement/{achie_slug}',
        ['as' => 'post.delete.achievements',
            'uses' => 'admin\AchievementsController@postDeleteAchievement']);

    Route::get('edit-achievement/{achie_slug}',
        ['as' => 'get.edit.achievements',
            'uses' => 'admin\AchievementsController@getEditAchievement']);

    Route::post('update-achievement/{ach_id}',
        ['as' => 'post.edited.achievement',
            'uses' => 'admin\AchievementsController@postEditedAchievement']);


    Route::get('/all-news',
        ['as' => 'get.news.list',
            'uses' => 'admin\NewsController@getNewsList']);


    Route::get('/add-news',
        ['as' => 'get.add.news',
            'uses' => 'admin\NewsController@getAddNews']);


    Route::post('/add-news',
        ['as' => 'post.add.news',
            'uses' => 'admin\NewsController@postAddNews']);

    Route::get('/delete-news/{news_slug}',
        ['as' => 'get.delete.news',
            'uses' => 'admin\NewsController@postDeleteNews']);

    Route::get('/edit-news/{news_slug}',
        ['as' => 'get.edit.news',
            'uses' => 'admin\NewsController@getEditNews']);


    Route::post('/edit-news/{news_slug}',
        ['as' => 'post.edited.news',
            'uses' => 'admin\NewsController@postEditedNews']);




    Route::get('/all-admins',
        ['as' => 'get.admin.list',
            'uses' => 'admin\AdminController@getAllAdminList']);

    Route::get('/add-admin',
        ['as' => 'add.new.admin',
            'uses' => 'admin\AdminController@getAddNewAdmin']);

    Route::post('/add-new-admin',
        ['as' => 'post.add.new.admin',
            'uses' => 'admin\AdminController@postAddNewAdmin']);


    Route::get('/all-schedule',
        ['as' => 'get.timeTable.list',
            'uses' => 'admin\TimeTableController@getTimeTableList']);

    Route::get('/add-schedule/{id}',
        ['as' => 'add.timeTable',
            'uses' => 'admin\TimeTableController@getAddNewBatch']);

    Route::post('/add-new-batch',
        ['as' => 'add.newBatch',
            'uses' => 'admin\TimeTableController@postAddNewBatch']);

    Route::post('/save-year-batch',
        ['as' => 'save.year.batch',
            'uses' => 'admin\TimeTableController@postSaveYearBatch']);

    Route::get('/schedule-table/{year}',
        ['as' => 'view.schedule.table',
            'uses' => 'admin\TimeTableController@getScheduleTable']);

    Route::get('/delete-schedule/{slug}',
        ['as' => 'delete.schedule.one',
            'uses' => 'admin\TimeTableController@postDeleteOneSchedule']);

    Route::get('/edit-schedule/{slug}',
        ['as' => 'edit.schedule.one',
            'uses' => 'admin\TimeTableController@getEditScheduleForm']);

    Route::post('/save-edited-schedule/{id}',
        ['as' => 'save.edited.schedule',
            'uses' => 'admin\TimeTableController@postEditScheduleForm']);




    Route::get('/all-results',
        ['as' => 'get.all.results',
            'uses' => 'admin\ResultsController@getAllResults']);

    Route::get('/add-result',
        ['as' => 'get.add.results',
            'uses' => 'admin\ResultsController@getAddResult']);

    Route::post('/add-new-result',
        ['as' => 'post.add.result',
            'uses' => 'admin\ResultsController@postAddResult']);

    Route::get('/delete-result/{id}',
        ['as' => 'get.delete.student.result',
            'uses' => 'admin\ResultsController@getDeleteResult']);

    Route::get('/edit-result/{id}',
        ['as' => 'get.edit.student.result',
            'uses' => 'admin\ResultsController@getEditResult']);


    Route::post('/save-edited-result/{id}',
        ['as' => 'post.edited.result',
            'uses' => 'admin\ResultsController@postEditedResult']);


    Route::get('/contact-messages',
        ['as' => 'get.contact.message',
            'uses' => 'admin\ContactMessageController@getAllContactMessages']);


    Route::get('/contact-messages/{id}',
        ['as' => 'get.delete.contact.message',
            'uses' => 'admin\ContactMessageController@getDeleteContactMessages']);


});

  });

每次我尝试登录时,都会将我重定向到同一个登录页面。请指导我这是错的。

1 个答案:

答案 0 :(得分:0)

您应该从中间件组remove web middleware使其正常工作。它自动适用于web.php(5.3)和routes.php(5.2.27及更高版本)内的所有路由,如果您手动添加它,它将破坏与会话相关的功能。