2014-04-29 64 views
0

我試圖創建一個像Laravel 4應用程序一樣的頁面。當用戶到達站點時,應提示他們登錄。一旦用戶登錄,視圖(而不是URL)將會切換,並且用戶將能夠看到信息,就像他們被認證一樣。Laravel Auth重定向到上一頁

我的HTML(如果authroized應h1顯示 「驗證」,如果不是,它顯示的登錄表單)

<div class="container"> 
     @if(Auth::check()) 
      <h1>Auth</h1> 
     @else 
      {{ Form::open(array('url'=>'login', 'method'=>'post')) }} 
      <div class="row"> 
       <div class="col-xs-12"> 
        <div class="form-group"> 
         {{ Form::label('email', 'Email Address') }} 
         {{ Form::text('email', Input::old('email'), array('class'=>'form-control', 'placeholder'=>'[email protected]')) }} 
        </div> 
       </div> 
      </div> 
      <div class="row"> 
       <div class="col-xs-12"> 
        <div class="form-group"> 
         {{ Form::label('password', 'Password') }} 
         {{ Form::password('password', array('class'=>'form-control')) }} 
        </div> 
       </div> 
      </div> 
      <div class="row"> 
       <div class="col-xs-12"> 
        {{ Form::submit('Log In', array('class'=>'btn btn-primary pull-right')) }} 
       </div> 
      </div> 
      {{ Form::close() }} 
     @endif 
    </div> 

控制器

class SiteController extends BaseController { 

    public function getIndex() 
    { 
     return View::make('index'); 
    } 

    public function postLogin() { 
     $email = Input::get('email'); 
     $password = Input::get('password'); 
     if (Auth::attempt(array('email'=>$email, 'password'=>$password))) 
     { 
      return Redirect::route('index'); 
     } 
    } 

} 

我的用戶模式是默認附帶Laravel 4.截至目前,我通過Auth::attempt並得到return Redirect::route('index');,但@if(Auth::check())似乎並沒有被解僱。相反,它會繼續向我顯示登錄形式。我在這裏做錯了什麼?

回答

0

我看不出這裏有什麼問題,但你需要確保所發生的事情,它看起來像你的認證會話沒有堅持,但可以肯定,你可以:

<?php 

class SiteController extends BaseController { 

    public function getIndex() 
    { 
     Log::info('index - authed: '. Auth::check() ? 'yes' : 'no'); 

     return View::make('index'); 
    } 

    public function postLogin() { 
     $email = Input::get('email'); 

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

     if (Auth::attempt(array('email'=>$email, 'password'=>$password))) 
     { 
      Log::info('postLogin - attempt successful'); 

      return Redirect::route('index'); 
     } 

     Log::info('postLogin - error on attempt'); 
    } 

} 

然後檢查日誌:

php artisan tail 
+0

我發現了這個問題!我將我的數據庫列設置爲'userId'而不是'id',並且它將所有內容都丟掉了。謝謝! –