skip to Main Content

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?

2

Answers


  1. 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

    Login or Signup to reply.
  2. It’s very simple just like we overriding any parent class function in derived class.

    Create your own class and extend your package class SEOMeta and re-declare function that you want to override and put your logic inside. Don’t forget to use namespace of your package class SEOMeta at upper your custom class.

    Now use your custom class instead of package class inside your controller.

    e.g

    use PathtoSEOMeta;
    
    class urclassname extends SEOMeta{
    
        public function overridemethodname(){
    
            // put ur logic here.
        }
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search