2014-12-02 51 views
1

嗨,我很新的laravel。我被要求在Laravel創建應用程序。現在開始,我正在開發登錄模塊。如何在laravel 4.2中創建登錄功能

基本要求

  • 顯示的登錄頁面,登陸頁面,當用戶登錄。
  • 給用戶憑據後,會議應當設置和用戶必須重定向到內頁。
  • 成功登錄後,登錄頁面不應顯示,除非註銷。

因此,爲了檢查登錄狀態,我在下面的filters.php中使用了過濾器。

App::before(function($request) 
{ 
    // $myApp Singleton object 
    App::singleton('myApp', function(){ 
     $app = new stdClass; 
     $app->title = "APD | Dealership Invoicing"; 
     if (Auth::check()) { 
      $app->user = Auth::User(); 
      $app->isLogedin = TRUE; 
     } 
     else 
     { 
      $app->isLogedin = FALSE; 
      $app->user = FALSE; 
     } 
     return $app; 
    }); 
    $app = App::make('myApp'); 
    View::share('myApp', $app); 
}); 

我實現了基於張貼在「http://heera.it/laravel-4-view-composer-master-layout#.VH280nvB25s」的博客上面的代碼。

當用戶從視圖中單擊登錄按鈕時,我將數據發送到控制器並檢查數據庫以及數據是否正確,然後我將用戶詳細信息放入會話並重定向到內部頁面。

控制器代碼

public function validateLogin() 
{ 
    $data = Input::all(); 
    $user_data = $this->validate_user_login($data); 
    if(is_array($user_data) && !empty($user_data) && count($user_data) > 0) 
    { 
     /* The below conversion is used, because there seems to be difficulty in returning the Arrays from the Eloquent ORM.*/ 
     $user_array = (array)$user_data[0]; 
     Session::put('user_data', $user_array);    
     return Redirect::to('/jobs'); 
    } 
} 

Route.php代碼

Route::get('/', function() 
{ 
    #return View::make('login/login'); 
    return Redirect::to('/login'); 
}); 

Route::get('/login', 'User[email protected]'); 

Route::post('/user/validate_login', '[email protected]'); 

Route::group(array('before' => 'auth'), function() 
{ 
    Route::get('/jobs', '[email protected]_list'); 
}); 

但我的問題是,重定向帶我回到登錄頁面。

問題

  • 我怎麼可以設置登錄狀態登錄後爲真?
  • 如何啓動會話。我在控制器中設置了會話密鑰,足以驗證用戶會話嗎?
  • 將來我必須開發相同的REST API,我必須爲Web和服務平臺使用相同的應用程序。那麼基於這種把控制放在過濾器中會在API調用中造成任何困難?
  • 我在哪裏可以在「Auth :: Check()」中找到Auth類和Check函數?

回答

0

我已經通過使用Jeffrey在「https://laracasts.com/series/laravel-from-scratch/episodes/15」中的教程實現了登錄功能。這是一個很好的解釋。我用傑弗裏解釋的方式改變了我寫的代碼。它效果很好。

我會給出簡短的登錄功能,我在視頻後建立。

路由器文件

Router.php 
---------- 

/* This route is used to show the login page, when there is no session created.*/ 

Route::group(array('before' => 'login'), function() 
{ 
    Route::get('login', '[email protected]'); 
}); 

/* This below route is used when user is clicked on the login button in the log in page. */ 

Route::post('/user/store','[email protected]'); 

過濾器文件

Filter.php 
---------- 
App::before(function($request) 
{ 
    // $myApp Singleton object 
    App::singleton('myApp', function(){ 
     $app = new stdClass; 
     $app->title = "APD | Dealership Invoicing"; 
     if (Auth::check()) { 
      $app->user = Auth::User(); 
      $app->isLogedin = TRUE; 
     } 
     else 
     { 
      $app->isLogedin = FALSE; 
      $app->user = FALSE; 
     } 
     return $app; 
    }); 
$app = App::make('myApp'); 
View::share('myApp', $app); 
}); 


App::after(function($request, $response) 
{ 
    /* The below headers are used to restrict the browser to cache the pages.   
    */ 
    $response->headers->set("Cache-Control","no-cache,no-store, must-revalidate"); 
    $response->headers->set("Pragma", "no-cache"); //HTTP 1.0 
    $response->headers->set("Expires"," Sat, 26 Jul 1986 05:00:00 GMT"); 
}); 

/* 
| Authentication Filters  
| 
| The following filters are used to verify that the user of the current 
| session is logged into this application. The "basic" filter easily 
| integrates HTTP Basic authentication for quick, simple checking. 
| 
*/ 

Route::filter('auth', function() 
{ 
    if (Auth::guest()) 
    { 
     if (Request::ajax()) 
     { 
      /*return Response::make('Unauthorized', 401);*/ 
      return Response::make('common.unauthorized'); 
     } 
     else 
     { 
      return Redirect::guest('login'); 
     } 
    } 
}); 

控制器的文件

UserController.php 
------------------ 
/** 
* The below function is used to show the login screen. 
*/ 
public function create() 
{ 
    /* 
     This helps us to restrict the display of login page when clicked on browser back button after login. 
    */ 

    $headers = array(); 
    $headers['Expires'] = 'Tue, 1 Jan 1980 00:00:00 GMT'; 
    $headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0'; 
    $headers['Pragma'] = 'no-cache'; 

    return Response::make(View::make('login.login'), 200, $headers); 
    //return View::make('login.login'); 
}  

public function store() 
{ 
    $input_data = Input::all(); 
    $credentials = array(
     'user_name' => htmlEncode(trim($input_data['user_name'])), 
     'password' => $input_data['password'], 
     'status' => 1 
    ); 

    /* Here I am calling a function in the parent class. My UserController is extending the BaseController. The code will be available below. */ 

    $loginStatus = $this->validateUserLogin($credentials); 

    if($loginStatus['status'] == 200) 
    { 
     $roleId = Auth::User()->role_id; 
     $loggedInUserId = Auth::User()->id; 
     $redirectPage = '/products'; 
     switch ($roleId) 
     { 
      case 'super': 
       $redirectPage = '/manage_users'; 
       break; 
      case 'admin': 
       $redirectPage = '/products'; 
       break;     
     } 
     return Redirect::to($redirectPage); 
    } 
    else 
    { 
     return Redirect::to('login')->with('status_data',$loginStatus); 
    } 
} 

基地控制器文件

BaseController.php 
------------------ 

protected function validateUserLogin($userData = '') 
{ 
    $this->return_array = array();   
    if(!empty($userData)) 
    { 
     if(Auth::attempt($userData)) 
     { 
      $this->return_array['status'] = 200; 
      $this->return_array['message'] = 'Login successfull.'; 
     } 
     else 
     { 
      $userData['status'] = 0; 
      if(Auth::validate($userData)) // This is to verify weather user is existed with status '0'. That means De-active user. 
      { 
       $this->return_array['status'] = 100; 
       $this->return_array['message'] = 'Your account is deactivated, Please contact your admin.'; 
      } 
      else 
      { 
       $this->return_array['status'] = 100; 
       $this->return_array['message'] = 'Login failed. Please enter valid user name and password.'; 
      } 
     } 
    } 
    else 
    { 
     $this->return_array['status'] = 100; 
     $this->return_array['message'] = 'Unable to login please try after some time.'; 
    } 

    return $this->return_array; 
} 
+0

我已經複製了此代碼但Auth :: check()不適用於我 – 2017-04-11 13:11:32