php – 将自定义函数添加到Auth类Laravel(扩展Guard类)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – 将自定义函数添加到Auth类Laravel(扩展Guard类),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1369字,纯文字阅读大概需要2分钟。
内容图文
![php – 将自定义函数添加到Auth类Laravel(扩展Guard类)](/upload/InfoBanner/zyjiaocheng/827/dd67677449e7403f815a84601e36abea.jpg)
我修改了Laravel的供应商文件
/vendor/laravel/framework/src/Illuminate/Auth/Guard.php
但是在更新Laravel时会被覆盖.
我正在寻找一种方法将代码放在我的/ app中,以防止覆盖.
修改的功能是
public function UpdateSession() {
$this->session->set('type', $type); //==> Set Client Type
}
此外,该文件还有一个新功能:
public function type() {
return $this->session->get('type'); //==> Get Client Type
}
上面的代码在我的应用程序的许多地方被调用.
任何的想法?
解决方法:
脚步:
1-创建myGuard.php
class myGuard extends Guard
{
public function login(Authenticatable $user, $remember = false)
{
$this->updateSession($user->getAuthIdentifier(), $user->type);
if ($remember) {
$this->createRememberTokenIfDoesntExist($user);
$this->queueRecallerCookie($user);
}
$this->fireLoginEvent($user, $remember);
$this->setUser($user);
}
protected function updateSession($id, $type = null)
{
$this->session->set($this->getName(), $id);
$this->session->set('type', $type);
$this->session->migrate(true);
}
public function type()
{
return $this->session->get('type');
}
}
2-在AppServiceProvider或新的服务提供者或routes.php中:
public function boot()
{
Auth::extend(
'customAuth',
function ($app) {
$model = $app['config']['auth.model'];
$provider = new EloquentUserProvider($app['hash'], $model);
return new myGuard($provider, App::make('session.store'));
}
);
}
在config / auth.php中为3-
'driver' => 'customAuth',
4-现在你可以使用它
Auth::type();
内容总结
以上是互联网集市为您收集整理的php – 将自定义函数添加到Auth类Laravel(扩展Guard类)全部内容,希望文章能够帮你解决php – 将自定义函数添加到Auth类Laravel(扩展Guard类)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。