dsepcxw181184853 2017-12-21 05:03
浏览 17
已采纳

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.

It looks like I need to alias the class in my App/Providers/AppServiceProvider

I've tried this:

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

But this doesn't do anything.

I can get my class registered fine:

 $this->app->register(
       '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

  • 写回答

1条回答 默认 最新

  • douzaoqu4447 2017-12-21 05:56
    关注

    So the solution was create a new ServiceProvider

    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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部