Laravel 5.7覆盖供应商类别并扩展旧的类别 [英] Laravel 5.7 Override vendor class and extend old one

查看:73
本文介绍了Laravel 5.7覆盖供应商类别并扩展旧的类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以解决方案是创建一个新的ServiceProvider.

So the solution was to create a new ServiceProvider.

此解决方案适用于覆盖

php artisan make:provider MyServiceProvider

扩展了供应商服务提供商(位于config/app.php中).在该ServiceProvider中,将我的别名添加到覆盖的 register 方法

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

现在,如何扩展覆盖的类?我尝试过:

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

推荐答案

您需要做的第一件事就是扩展Vendor类:

First thing you need to do is extend the Vendor class:

class MyCustomClass extends Vendor\VendorName\Class {}

现在,此类具有Vendor类的属性和方法以及您添加的属性和方法.

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

这篇关于Laravel 5.7覆盖供应商类别并扩展旧的类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆