【发布时间】:2017-09-11 08:57:55
【问题描述】:
我正在使用 https://github.com/artesaos/seotools 包进行 seo。 我正在尝试覆盖位于 https://github.com/artesaos/seotools/blob/master/src/SEOTools/SEOMeta.php 的 getCanonical 函数并将其输出为小写。你能指导我怎么做吗?
【问题讨论】:
我正在使用 https://github.com/artesaos/seotools 包进行 seo。 我正在尝试覆盖位于 https://github.com/artesaos/seotools/blob/master/src/SEOTools/SEOMeta.php 的 getCanonical 函数并将其输出为小写。你能指导我怎么做吗?
【问题讨论】:
这很简单,就像我们在派生类中重写任何父类函数一样。
创建您自己的类并扩展您的包类SEOMeta 并重新声明您想要覆盖的函数并将您的逻辑放入其中。不要忘记在自定义类的上层使用包类的命名空间SEOMeta。
现在使用您的自定义类而不是控制器中的包类。
例如
use Path\to\SEOMeta;
class urclassname extends SEOMeta{
public function overridemethodname(){
// put ur logic here.
}
}
【讨论】:
您可以尝试以下操作:
第 1 步:
创建一个扩展 SEOMeta 类的子类并覆盖 getCanonical 函数。
Class XyzSEOMeta extends SEOMeta {
public function getCanonical () {
// Write your logic here
}
}
第 2 步:
为被覆盖的类创建服务提供者。 bind 函数的第一个参数必须与SEOMeta Facade (check here) 的外观访问器相同。在 seotools 包的服务提供者之后,在config/app.php 注册这个门面。 :
Class XyzSEOMetaServiceProvider extends ServiceProvider {
public function register(){
$this->app->bind('seotools.metatags', function(){
return new XyzSEOMeta($this->app['config']);
})
}
}
你已经准备好了。希望这会有所帮助。
编辑:
上面提到的方法只会覆盖单个类。如果要更改多个类的逻辑。最好的方法是分叉项目。更改代码并将其推送到您的 fork。使用分叉项目作为您的作曲家依赖项。点击链接了解如何使用私有存储库作为作曲家依赖:https://getcomposer.org/doc/05-repositories.md#using-private-repositories
【讨论】:
{!! SEO::generate() !!},我在配置下有 seotool.php,设置如下:'canonical' => null, // Set null for using Url::current()