简体   繁体   中英

Laravel 5.7 Override vendor class and extend old one

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

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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM