Laravel 5.7 Override vendor class and extend old one

六月ゝ 毕业季﹏ 提交于 2020-02-29 04:28:20

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!