Laravel 5.5覆盖供应商类 [英] Laravel 5.5 Override vendor class

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

问题描述

我正在尝试覆盖(而不是扩展)供应商类.因此,每当调用供应商类时(在供应商代码内),我都希望它调用我的自定义类.

I'm trying to override (not extend) a vendor class. So whenever the vendor class gets called (within the vendor code), I want it to call my custom class.

似乎我需要在App/Providers/AppServiceProvider

我已经尝试过了:

$loader = \Illuminate\Foundation\AliasLoader::getInstance();
$loader->alias('Vendor\VendorName\Class', 'App\Vendor\MyCustomClass');

但是这什么也没做.

我可以让我的班级注册得很好:

I can get my class registered fine:

 $this->app->register(
       'App\Vendor\MyCustomClass'
   );

但是这失败了,因为构造函数依赖于当时不可用的其他变量.我实际上只是需要该应用程序用App\Vendor\MyCustomClass

But this then fails as the constructor relys on other variables not available at that point. I literally just need the app to override any call to Vendor\VendorName\Class with App\Vendor\MyCustomClass

推荐答案

因此解决方案是创建一个新的ServiceProvider

So the solution was 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');

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

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