问题
So the solution was to create a new ServiceProvider.
This solution works for Override
php artisan make:provider MyServiceProvider
Which extended the Vendor service provider (found within config/app.php). Within that ServiceProvider, add my alias within the overridden register method
$loader->alias('Vendor\VendorName\Class', 'App\Vendor\MyCustomClass');
https://stackoverflow.com/a/47926486/10589868
Now, how do I extend the overridden class? I tried this:
$loader->alias('ClassParent', 'Vendor\VendorName\Class');
$loader->alias('Vendor\VendorName\Class', 'App\Vendor\MyCustomClass');
...
class MyCustomClass extends ClassParent {} // not working
回答1:
First thing you need to do is extend the Vendor class:
class MyCustomClass extends Vendor\VendorName\Class {}
Now, this class has the properties and methods of the Vendor class and the properties and methods you've added.
Then, your custom class can become an alias:
$loader->alias('App\Vendor\MyCustomClass', 'Vendor\VendorName\Class');
来源:https://stackoverflow.com/questions/53097256/laravel-5-7-override-vendor-class-and-extend-old-one