[英]How to extend Laravel's Auth Guard class?
I'm trying to extend Laravel's Auth Guard class 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 Framework I'm stuck on how to exactly do this because the Guard class itself doesn't have an own IoC binding which 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 eg Auth::check()
? 现在我应该如何注册要使用的扩展类
Foobar\\Extensions\\Auth\\Guard
而不是原始的Illuminate\\Auth\\Guard
这样我就可以像Auth::check()
一样调用Auth::myCustomMethod()
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. 一种方法是替换
app/config/app.php
的Auth
别名,但我不确定这是否真的是解决此问题的最佳方法。
BTW: I'm using Laravel 4.1. 顺便说一句:我正在使用Laravel 4.1。
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
). 我建议您创建自己的服务提供程序,或直接在其中一个启动文件中扩展Auth类(例如,
start/global.php
)。
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,但使用自定义提供程序。
\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`。
Only way to add (and also replace existing functions) is to create copy of Guard.php file in your project and in app/start/global.php add: 添加(以及替换现有函数)的唯一方法是在项目中创建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' ... 当然这是一种丑陋的方法,但我花了一个多小时来测试所有可能性[如何通过Auth更改存储在Session中的内容]并且它总是以错误结束:...类HSGuard的构造要求第一个参数为'UserProviderInterface'并获得'EloquentUserProvider'......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.