beforeFilter上的身份验证laravel 4(' auth')不起作用

时间:2014-08-25 10:54:15

标签: php authentication laravel-4 laravel-routing before-filter

我有一些控制器,我想从登录页面保护这个,我添加登录页面 而我的控制器我添加__construct来保护这个

看我的代码:

在NewsController中我想要来自身份验证的安全getindex页面

class NewsController extends Basecontroller{

    public function __construct() {
   $this->beforeFilter('csrf', array('on'=>'post'));
   $this->beforeFilter('auth');
}

并在路线文件中添加以下代码:

Route::post('login',function()
{

 //print_r(Hash::make(Input::get('UserName')));

 if (Auth::attempt(array('UserName'=>Input::get('UserName'), 'password'=>Input::get('Password')))) {
  return Redirect::to('news')->with('message', 'You are now logged in!');
  } else {
  return Redirect::to('login')
    ->with('message', 'Your username/password combination was incorrect')
    ->withInput();
}

我确定用户传递是corect并已通过但是在控制器中重定向我的登录页面

出了什么问题?

我的完整route.php文件

Route::get('login',function(){
 return View::make('Admin/Login');

 });
Route::post('login',function()
{


if (Auth::attempt(array('UserName'=>Input::get('UserName'),     'password'=>Input::get('Password')))) {
    return Redirect::to('news')->with('message', 'You are now logged in!');
} else {
return Redirect::to('login')
    ->with('message', 'Your username/password combination was incorrect')
    ->withInput();
}

});


Route::controller('news', 'NewsController');
Route::controller('article', 'ArticleController');
Route::controller('albume', 'AlbumeController');
Route::controller('staticcontent', 'StaticContentController');
Route::controller('links', 'LinksController');
Route::controller('faq', 'FAQController');
Route::controller('employment', 'EmploymentController');
Route::controller('poll', 'PollController');
Route::controller('branches', 'BranchesController');
Route::controller('user', 'UserController');
Route::controller('access', 'AccessLevelController');
Route::controller('metatag', 'MetaTagController');
Route::controller('product', 'ProductController');

并获取登录视图:

{{Form::open(array('method'=>'post','id'=>'Filter'))}}
<table class="Login">
<tr>
<td>user</td>
<td>{{Form::text('UserName')}}</td>
</tr>
<tr>
<td>pass</td>
<td>{{Form::password('Password')}}</td>
</tr>
<tr>
<td colspan="2">{{HTML::link('','forget pass ?')}}</td>
</tr>
<tr>
<td colspan="2">{{Form::submit('login')}}</td>
</tr>
</table>
{{Form::close()}}

1 个答案:

答案 0 :(得分:0)

我找到了答案

我想将用户表ID添加到Model User.php

protected $primaryKey = "ID";