Laravel 5软件包开发-Auth :: attempt()导致App \ User找不到 [英] Laravel 5 package development - Auth::attempt() leads to App\User not found
问题描述
我正在开发一个包装一些Laravel User逻辑的程序包,因为我想在添加一些功能的同时保留本机实现.
I'm developing a package that wraps some of the Laravel User logic, because i want to keep the native implementation while adding some feature to it.
这是我包的composer.json
This is my package's composer.json
{
"name": "foo/bar",
"description": "A Laravel 5 package",
"authors": [
{
"name": "",
"email": ""
}
],
"require": {
"php": ">=5.4.0",
"illuminate/auth": "5.1.*"
},
"require-dev": {
"orchestra/testbench": "~3.0"
},
"autoload": {
"psr-4": {
"Foo\\": "src/Foo/"
}
},
"minimum-stability": "stable"
}
这是示例代码:
class Foo {
function login($username, $password) {
\Auth::attempt($username, $password);
}
}
这是一个示例测试用例
class UserServiceTest extends \Orchestra\Testbench\TestCase {
function testLogin() {
$foo = new Foo();
$foo->login('foo', 'bar');
}
}
现在,在运行测试时,我得到了这个PHP Fatal error: Class '\App\User' not found in path/to/laravel/framework/src/Illuminate/Auth/EloquentUserProvider.php on line 126
Now, when running the test i get this PHP Fatal error: Class '\App\User' not found in path/to/laravel/framework/src/Illuminate/Auth/EloquentUserProvider.php on line 126
我可以理解为什么(App \ User位于laravel/laravel
包中),但是我无法理解如何正确声明我的包依赖项.
I can understand why (App\User lives in the laravel/laravel
package), however i cannot understand how to properly declare my package dependencies.
我该怎么做才能重用整个Auth和User laravel本机实现?
What should i do in order to reuse the whole Auth and User laravel native implementation?
推荐答案
您无需使用 laravel/laravel 包即可重复使用Laravel的身份验证.
You don't need to use laravel/laravel package in order to reuse Laravel's authentication.
仅导入 illuminate/auth 包就足够了.这将为您提供3个验证用户身份的东西中的2个:
It's enough to just import illuminate/auth package. This will give you 2 out of 3 things that you need to authenticate users:
- Guard 类(通常通过 Auth 外观访问),提供了身份验证方法(尝试,检查等)
- 用户提供程序,它根据用户的凭据获取用户-此软件包同时提供了Eloquent和数据库用户提供程序
- Guard class (usually accessed via Auth facade) that provides authentication method (attempt, check, etc.)
- User provider that fetches users based on their credentials - this package provides both Eloquent and Database user provider
您最后需要做的是User类或实现 AuthenticatableContract 的任何模型.
The last thing you need is a User class, or any Model that implements AuthenticatableContract.
Guard 将用户提供程序作为其构造函数的参数之一,而用户提供程序将模型的类用作其构造函数的参数之一构造函数的参数.
Guard takes user provider as one of its constructor's arguments, and user provider takes the class of a model to use as one of its constructor's arguments.
这3个元素足以使用Laravel的身份验证.
Those 3 elements are enough to use Laravel's authentication.
这篇关于Laravel 5软件包开发-Auth :: attempt()导致App \ User找不到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!