Strategy to override a class in a library installed with Composer
In composer.json, under ["autoload"]["psr-4"], add an entry with namespace as the key and path as the value:
{
"autoload": {
"psr-4": {
"BuggyVendor\\Namespace\\": "myfixes/BuggyVendor/Namespace"
}
}
}
Copy files you want to override under that path (keeping sub-namespace directory structure) and edit them there. They will be picked in preference to the library package's original "classpath". It would seem that namespace->path mappings added to composer.json in this manner are considered before those added by required packages. Note: I just tried it and it worked, though I don't know if it is an intended feature or what possible gotchas are.
EDIT: found a gotcha. Sometimes when you subsequently require another package with composer require vendor/package
, you will "lose" the override. If this happens, you must issue composer dump-autoload
manually. This will restore the correct autoload order honoring your override.
How to override a Laravel package function
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
Laravel 6-7 How Can I Override/Change a Vendor Class?
I resulted to using composer to ovveride the file instead, as my previous method was not working whatsoever. Here is what I did.
Firstly, in the autoload section of the composer.json
file I added this:
"exclude-from-classmap": [
"vendor\\laravel\\framework\\src\\Illuminate\\Routing\\CompiledRouteCollection.php"
],
"psr-4": {
"App\\": "app/",
"Illuminate\\": "app/Overrides/"
},
Next I created the Ovverides folder in the app folder and then I copied the CompiledRouteCollection.php
file and pasted it inside of the Ovverides folder.
Related Topics
Why Are Escape Characters Being Added to the Value of the Hidden Input
Does PHP Close the File After the File Handler Is Garbage Collected
Fatal Error: Call to Undefined Function: Ldap_Connect()
Permission Denied Despite Appropriate Permissions Using PHP
Insert Non-English Decimal Points in MySQL
PHP Date Showing '1970-01-01 ' After Conversion
How to Enable Shell_Exec and Exec on PHP
This Is the .Htaccess Code in Wordpress. Can Someone Explain How It Works
JSON_Encode Produce JSON_Error_Utf8 from Mssql-Select
Fatal Error: Call to Undefined Function Mcrypt_Encrypt()
PHP Method="Post" Stopped Working After I Added This .Htaccess... Why
Find Duplicate Value in Multi-Dimensional Array
Does PHP Run Faster Without Warnings
PHP SQL Select Where Like Search Item with Multiple Words
Troubleshooting "No Such File or Directory" When Running 'PHP App/Console Doctrine:Schema:Create'
What Rss Parser Should I Use in PHP