php - laravel7 覆盖供应商包类
问题描述
一般来说,我是这个服务容器的新手。
只是寻找一种简单的方法来覆盖类的getView()方法Captcha
。
我的想法是创建一个扩展类的新captcha
类:
<?php
namespace App\Http\Helpers\Captcha;
use Igoshev\Captcha\Captcha\Storage\StorageInterface;
use Igoshev\Captcha\Captcha\Generator\GeneratorInterface;
use Igoshev\Captcha\Captcha\Code\CodeInterface;
class CaptchaNew extends Igoshev\Captcha\Captcha
{
/**
* Get html image tag.
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function getView()
{
//new code...
}
}
在AppServiceProvider
下面的register
方法中使用:
$loader = AliasLoader::getInstance();
$loader->alias('App\Http\Helpers\Captcha\CaptchaNew', 'Igoshev\Captcha\Captcha');
这个方法我也试过了boot
,还是不行。覆盖类的最佳方法是什么?还提供了一个“服务提供者”,但我想让事情变得简单,我对服务提供者一无所知。
解决方案
您可以在下面尝试这种方式来注册您的服务,改用绑定功能。在 AppServiceProvider 中的函数下,将您的register
函数替换为下面的示例代码。但请注意,绑定函数的第一个参数必须与供应商库的外观访问器相同(本例为\Igoshev\Captcha\Captcha\Captcha::class,您可以看这里)。在 config/app.php 中注册这个门面在 package 的 service provider 之后:
$this->app->bind(\Igoshev\Captcha\Captcha\Captcha::class, function(){
return new CaptchaNew($this->app['config']);
})
推荐阅读
- python - SUMO 和 TraCI,多个客户端 - 一个连接
- javascript - Vue js在编译时正在删除脚本标签
- powershell - 对包含给定短语的字符串应用过滤器并返回之后发生的所有内容
- javascript - 根据另一个单词数组对一组单词进行排序
- c# - CodeDOM:命名参数
- node.js - node.js @sendgrid/mail 错误:未经授权
- kubernetes - kube-apiserver healthz 调用中检查了什么?
- css - 当我知道值不是空白时,Asp.net 参数显示为空
- eclipse-emf - 如何在同一工作空间中而不是在运行时 Eclipse 环境中从 Ecore 元模型实例化模型?
- jquery - 单击链接时删除超链接