2013-04-04 22 views
2

沒有發現我不斷收到類「用戶」在Laravel

Unhandled Exception 
Message: 

Class 'User' not found 

Location: 

C:\wamp\www\laravel\laravel\auth\drivers\eloquent.php on line 70 

當我在用戶登錄我。我不認爲eloquent.php中有任何問題。請看看我的登錄控制器

class Login_Controller extends Base_Controller { 

    public $restful = true; 

    public function get_index(){ 
     return View::make('login'); 
    } 

    public function post_index(){ 

     $username = Input::get('username'); 
     $password = Input::get('password'); 
     $user_details = array('username' => $username, 'password' => $password); 

     if (Auth::attempt($user_details)) 
     { 
      return Redirect::to('home.index'); 
     } 
     else 
     { 
      return Redirect::to('login') 
       ->with('login_errors', true); 
     } 


    } 
} 

這是登錄的視圖

{{ Form::open('login') }} 
    <!-- username field --> 
    <p>{{ Form::label('username', 'Username') }}</p> 
    <p>{{ Form::text('username') }}</p> 
    <!-- password field --> 
    <p>{{ Form::label('password', 'Password') }}</p> 
    <p>{{ Form::password('password') }}</p> 
    <!-- submit button --> 
    <p>{{ Form::submit('Login', array('class' => 'btn btn-primary')) }}</p> 
{{ Form::close() }} 

而且routes.php文件

<?php 

Route::controller(Controller::detect()); // This line will map all our requests to all the controllers. If the controller or actions don’t exist, the system will return a 404 response. 
Route::get('about', '[email protected]'); 


Route::filter('auth', function() 
{ 
    if (Auth::guest()) return Redirect::to('login'); 
}); 

我用作爲我的身份驗證驅動程序雄辯。我嘗試將其更改爲Fluent,但在單擊登錄按鈕後,它會在else語句中顯示此行return Redirect::to('login')->with('login_errors', true);發出的登錄錯誤。

使用Eloquent時,'User'類有什麼問題?

+1

你的例子。我沒有看到a:'class User {...}'所以它沒有定義 – 2013-04-04 02:00:39

+0

的確,'User'應該是一個模型。請確認您的'models'文件夾中有'user.php'文件。 – 2013-04-04 04:41:47

回答

4

邁克的權利,這是因爲你有沒有用戶的模式,但還有更多......

所在行laravel的用戶模型搜索並沒有發現它是這樣的:

if (Auth::attempt($user_details)) 

發生這種情況是因爲Laravels身份驗證系統默認使用了雄辯的驅動程序。爲了滿足Laravel,你需要一個名爲'users'的數據庫表,至少包含文本類型的列'用戶名'和'密碼',也可以是使用時間戳時的'created_at'和'updated_at'列,但可以將其關閉。

\application\models\user.php 

<?PHP 
class User extends Eloquent 
{  
    public static $timestamps = false; //I don't like 'em ;) 
} 
2

這實際上是對@Hexodus響應的評論,但我沒有要求評論的要點。

實際上,你可以有你的用戶身份驗證命名任何你想要的,例如

  • 型號:Turtle
  • 表:turtles

但你必須進入app\config\auth.php並更改'model' => '...''table' => '...'值以使Laravel的身份驗證正常工作。

此外,according to the docs,你甚至不需要'username''password'明確定義爲這樣的數據庫中的

if (Auth::attempt(array('email' => $email, 'password' => $password))) 
{ 
    return Redirect::intended('dashboard'); 
} 

注意到,「電子郵件」不是一個必需的選項,它僅用於例。您應該在數據庫中使用與「用戶名」相對應的任何列名稱。重定向::預期功能會將用戶重定向到他們在被認證過濾器捕獲之前嘗試訪問的URL。如果預期的目的地不可用,則可以爲此方法提供回退URI。

實際上,在這種情況下'email'被認爲是'username'


編輯,因爲我是在第一次有這個麻煩,你需要時使用Auth::attempt(...)哈希密碼。