如何重写Laravel包功能 [英] How to override a Laravel package function
问题描述
我正在将 https://github.com/artesaos/seotools 软件包用于seo.我试图覆盖位于 https://github上的getCanonical函数..com/artesaos/seotools/blob/master/src/SEOTools/SEOMeta.php 并将其输出为小写.你能指导我怎么做吗?
I'm using https://github.com/artesaos/seotools package for seo. I am trying to override getCanonical function located at https://github.com/artesaos/seotools/blob/master/src/SEOTools/SEOMeta.php and make it's output as lowercase. Could you please guide me how can I do this?
推荐答案
您可以尝试以下操作:
第1步:
创建扩展SEOMeta类的子类,并覆盖 getCanonical
函数.
Create a child class extending SEOMeta class and override the getCanonical
function.
Class XyzSEOMeta extends SEOMeta {
public function getCanonical () {
// Write your logic here
}
}
第2步:
为覆盖的类创建服务提供者. bind
函数的第一个参数必须与 SEOMeta
Facade的外观访问器相同(
Create the Service Provider for overridden class. First parameter of bind
function must be same as facade accessor of SEOMeta
Facade (check here). Register this facade in config/app.php
after the service provider of seotools package. :
Class XyzSEOMetaServiceProvider extends ServiceProvider {
public function register(){
$this->app->bind('seotools.metatags', function(){
return new XyzSEOMeta($this->app['config']);
})
}
}
您都准备好了.希望这会有所帮助.
You are all set. Hope this will help.
上面提到的方法将只覆盖单个类.如果要更改多个类的逻辑.最好的方法是分叉项目.更改代码并将其推送到您的fork.使用派生项目作为您的作曲家依赖项.单击链接了解如何将私有存储库用作作曲家依赖项: https://getcomposer.org/doc/05-repositories.md#using-private-repositories
Above mention method will just override the single class. If you want to change the logic of more than one class. Best way is to fork the project. Change the code and push it to your fork. Use forked project as your composer dependency. Follow the link to know how to use private repository as composer dependency : https://getcomposer.org/doc/05-repositories.md#using-private-repositories
这篇关于如何重写Laravel包功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!