如何扩展 Laravel 的 Auth Guard 类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20701322/
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
How to extend Laravel's Auth Guard class?
提问by Holger Weis
I'm trying to extend Laravel's Auth Guardclass by one additional method, so I'm able to call Auth::myCustomMethod()
at the end.
我正在尝试通过一种附加方法扩展 Laravel 的 Auth Guard类,因此我可以Auth::myCustomMethod()
在最后调用。
Following the documentation section Extending The FrameworkI'm stuck on how to exactly do this because the Guard class itself doesn't have an own IoC bindingwhich I could override.
在文档部分扩展框架之后,我被困在如何准确地执行此操作上,因为 Guard 类本身没有我可以覆盖的自己的IoC 绑定。
Here is some code demonstrating what I'm trying to do:
这是一些代码,演示了我正在尝试做的事情:
namespace Foobar\Extensions\Auth;
class Guard extends \Illuminate\Auth\Guard {
public function myCustomMethod()
{
// ...
}
}
Now how should I register the extended class Foobar\Extensions\Auth\Guard
to be used instead of the original Illuminate\Auth\Guard
so I'm able to call Auth::myCustomMethod()
the same way as e.g. Auth::check()
?
现在我应该如何注册Foobar\Extensions\Auth\Guard
要使用的扩展类而不是原始类,Illuminate\Auth\Guard
以便我能够以Auth::myCustomMethod()
与 eg 相同的方式调用Auth::check()
?
One way would be to replace the Auth
alias in the app/config/app.php
but I'm not sure if this is really the best way to solve this.
一种方法是替换中的Auth
别名,app/config/app.php
但我不确定这是否真的是解决此问题的最佳方法。
BTW: I'm using Laravel 4.1.
顺便说一句:我使用的是 Laravel 4.1。
回答by David Barker
I would create my own UserProvider service that contain the methods I want and then extend Auth.
我将创建自己的 UserProvider 服务,其中包含我想要的方法,然后扩展 Auth。
I recommend creating your own service provider, or straight up extending the Auth class in one of the start files (eg. start/global.php
).
我建议创建您自己的服务提供者,或者直接在启动文件之一(例如start/global.php
)中扩展 Auth 类。
Auth::extend('nonDescriptAuth', function()
{
return new Guard(
new NonDescriptUserProvider(),
App::make('session.store')
);
});
This is a good tutorial you can follow to get a better understanding
这是一个很好的教程,您可以遵循以更好地理解
There is another method you could use. It would involve extending one of the current providers such as Eloquent.
您可以使用另一种方法。它将涉及扩展当前的提供程序之一,例如 Eloquent。
class MyProvider extends Illuminate\Auth\EloquentUserProvider {
public function myCustomMethod()
{
// Does something 'Authy'
}
}
Then you could just extend auth as above but with your custom provider.
然后你可以像上面一样扩展身份验证,但使用你的自定义提供者。
\Auth::extend('nonDescriptAuth', function()
{
return new \Illuminate\Auth\Guard(
new MyProvider(
new \Illuminate\Hashing\BcryptHasher,
\Config::get('auth.model')
),
\App::make('session.store')
);
});
Once you've implemented the code you would change the driver in the auth.php
config file to use 'nonDescriptAuth`.
实现代码后,您将更改auth.php
配置文件中的驱动程序以使用“nonDescriptAuth”。
回答by Jerzy Skalski
Only way to add (and also replace existing functions) is to create copy of Guard.phpfile in your project and in app/start/global.phpadd:
添加(并替换现有函数)的唯一方法是在您的项目中创建Guard.php文件的副本,并在app/start/global.php添加:
require app_path().'/models/Guard.php';
Of course it's ugly method, but I spent over hour to test all possibilities [how to change things stored in Session by Auth] and it always end with error: ... _contruct of class HSGuard requires first parameter to be 'UserProviderInterface' and get 'EloquentUserProvider' ...
当然这是丑陋的方法,但我花了一个多小时来测试所有可能性[如何通过身份验证更改存储在会话中的内容],它总是以错误结束:... HSGuard 类的 _contruct 需要第一个参数为“UserProviderInterface”并获取'EloquentUserProvider' ...