Laravel基本认证 [英] Laravel basic-auth
问题描述
我想在网页上使用basic.auth
,但是身份验证不起作用
I want to use basic.auth
for my web page but authentication donst work
Route::get('admin', array('before' => 'auth.basic', function()
{
return 'Top secret';
}));
create
-创建测试用户
create
- create test user
Route::get('create', function()
{
$user = new User;
$user->email = 'test@test.com';
$user->username = 'test';
$user->password = Hash::make('password');
$user->save();
});
config
-
app/config/app
-定义了key
(创建了Laravel安装) -
app/config/auth
-定义了model
(User
)和table
(users
) app/config/app
- has definedkey
(that created Laravel installation)app/config/auth
- has definedmodel
(User
) andtable
(users
)
config
Route::filter('auth.basic', function()
{
return Auth::basic();
});
测试
我呼叫/create
创建用户test@test.com
:password
以下是users
表:
然后我打电话给/admin
进行登录
Then I call /admin
to login
但是它不让我进去.登录之后-它只是清除输入. 取消之后-返回Invalid credentials.
.
But it doesnt let me in. After Login - it just clear inputs. After Cancel - it return Invalid credentials.
.
我尝试实施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;
}
}
问题解决了
我在User
模型return $this->passsword;
中有错字.有3个s
.
Problem solved
I had typo in User
model return $this->passsword;
There is 3 s
.
现在,我使用默认的Laravel 用户模型
Now I use default Laravel User model.
推荐答案
确保在app/config/auth.php中-driver
设置为eloquent
.
Ensure that in app/config/auth.php - driver
is set to eloquent
.
您可能还需要实现UserInterface
接口(class User extends Eloquent implements UserInterface
)-然后需要在模型中包括方法:
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:
/**
* 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;
}
这篇关于Laravel基本认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!