Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override a Laravel package function

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?

like image 521
Mehdi Avatar asked Oct 28 '25 05:10

Mehdi


1 Answers

You can try following :

Step 1:

Create a child class extending SEOMeta class and override the getCanonical function.

Class XyzSEOMeta extends SEOMeta {
    public function getCanonical () {
       // Write your logic here
    }
}

Step 2:

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.

EDIT:

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

like image 177
Pankit Gami Avatar answered Oct 29 '25 19:10

Pankit Gami



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!