简体   繁体   English

Laravel 5.7覆盖供应商类并扩展旧类

[英]Laravel 5.7 Override vendor class and extend old one

So the solution was to create a new ServiceProvider. 所以解决方案是创建一个新的ServiceProvider。

This solution works for Override 此解决方案适用于Override

php artisan make:provider MyServiceProvider

Which extended the Vendor service provider (found within config/app.php). 这扩展了供应商服务提供商(在config / app.php中找到)。 Within that ServiceProvider, add my alias within the overridden register method 在该ServiceProvider中,在重写的寄存器方法中添加我的别名

 $loader->alias('Vendor\VendorName\Class', 'App\Vendor\MyCustomClass');

https://stackoverflow.com/a/47926486/10589868 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: 您需要做的第一件事是扩展Vendor类:

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. 现在,此类具有Vendor类的属性和方法以及您添加的属性和方法。

Then, your custom class can become an alias: 然后,您的自定义类可以成为别名:

 $loader->alias('App\Vendor\MyCustomClass', 'Vendor\VendorName\Class');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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