Laravel 基本身份验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17447617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel basic-auth
提问by Shaddow
I want to use basic.auth
for my web page but authentication donst work
我想basic.auth
用于我的网页,但身份验证不起作用
routes.php
路由文件
admin
- authentication
admin
- 验证
Route::get('admin', array('before' => 'auth.basic', function()
{
return 'Top secret';
}));
create
- create test user
create
- 创建测试用户
Route::get('create', function()
{
$user = new User;
$user->email = '[email protected]';
$user->username = 'test';
$user->password = Hash::make('password');
$user->save();
});
config
配置
app/config/app
- has definedkey
(that created Laravel installation)app/config/auth
- has definedmodel
(User
) andtable
(users
)
app/config/app
- 已定义key
(创建 Laravel 安装)app/config/auth
- 已定义model
(User
) 和table
(users
)
filters.php
过滤器.php
auth.basic
auth.basic
Route::filter('auth.basic', function()
{
return Auth::basic();
});
test
测试
I call /create
to create User [email protected]
:password
我打电话/create
创建用户[email protected]
:password
Here is users
table after:
这是users
之后的表格:
Then I call /admin
to login
然后我打电话/admin
登录
But it doesnt let me in. After Login- it just clear inputs. After Cancel- it return Invalid credentials.
.
但它并没有让我进去。之后Login- 它只是清除输入。After Cancel- 它返回Invalid credentials.
。
User model
用户模型
I tried implement UserInterface
我试过实现 UserInterface
<?php
use Illuminate\Auth\UserInterface;
class User extends Eloquent implements UserInterface {
protected $table = 'users';
/**
* Get the unique identifier for the user.
*
* @return mixed
*/
public function getAuthIdentifier()
{
return $this->getKey();
}
/**
* Get the password for the user.
*
* @return string
*/
public function getAuthPassword()
{
return $this->passsword;
}
}
Problem solved
问题解决了
I had typo in User
model return $this->passsword;
There is 3 s
.
我在User
模型中return $this->passsword;
有错别字There is 3 s
。
Now I use default Laravel User model.
现在我使用默认的 Laravel User 模型。
回答by Half Crazed
Ensure that in app/config/auth.php - driver
is set to eloquent
.
确保在 app/config/auth.php -driver
设置为eloquent
.
You may also need to implement the UserInterface
interface (class User extends Eloquent implements UserInterface
) - then you'll need to include the methods in your model:
您可能还需要实现UserInterface
接口 ( class User extends Eloquent implements UserInterface
) - 然后您需要在模型中包含这些方法:
/**
* Get the unique identifier for the user.
*
* @return mixed
*/
public function getAuthIdentifier()
{
return $this->getKey();
}
/**
* Get the password for the user.
*
* @return string
*/
public function getAuthPassword()
{
return $this->password;
}