2016-11-02 39 views
2

您好我在laravel身份驗證有一些問題。我必須使用兩個中間件1.是web和2. auth。我正在使用網絡中間件,以便我可以使用會話來顯示Flash消息。並希望使用auth中間件來執行用戶/管理員的身份驗證。但我正面臨一些問題。中間件身份驗證不與網絡Laravel 5.2一起工作

下面

是我的函數來檢查授權,並重定向到各自的路線

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' => '[email protected]']); 

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


Route::get('/register', 
    ['as' => 'get.register.page', 
     'uses' => '[email protected]']); 

Route::post('/register', 
    ['as' => 'post.register.form', 
     'uses' => '[email protected]']); 

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



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


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


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

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

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


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

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

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


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


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


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

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

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


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




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

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

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


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

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

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

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

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

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

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

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




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

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

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

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

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


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


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


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


}); 

    }); 

每次我嘗試登錄它的時候重定向我一樣登錄頁面。請指導我這是怎麼回事。

+0

刪除其中一個 => return'admin other view'; => return Redirect :: route('get.admin.dashBoard'); 當它返回「管理其他視圖」,比控制消失,並且它不會執行第二個'返回'行 –

回答

0

你應該從中間件組remove web middleware使它工作。它適用於web.php(5.3)和routes.php(5.2.27及更高版本)內部的所有路由,如果您手動添加它,則會破壞與會話相關的功能。

+0

My Laravel Framework版本5.2.45。當我刪除Web中間件時,它不允許使用Session:flash來顯示Flash消息。 –

+0

@pawankumar,如果你的路線在'routes.php'中,你應該刪除'web'中間件。自從'5.2.27'以來,該中間件自動應用於所有路由。請點擊上面答案中的鏈接。 –

+0

假設我的某些路由不需要授權。 ?? –