如何在Laravel容器中换出依赖项 [英] How to swap out dependency in laravel container

查看:59
本文介绍了如何在Laravel容器中换出依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经注册了贝宝服务提供商:

I have registered a Paypal service provider:

App\Providers\PaypalHelperServiceProvider::class,

,当我在控制器中键入提示时,它会正确解析:

and, when I type hint it in my controller it properly resolves:

public function refund(Request $request, PaypalHelper $paypal) {...

这是我的提供程序类:

class PaypalHelperServiceProvider extends ServiceProvider
{
  protected $defer = true;

  public function register()
  {
      $this->app->bind('App\Helpers\PaypalHelper', function() {
          $test = 'test';
          return new PaypalHelper();
      });
    }

    public function provides()
    {
      $test = 'test';
      return [App\Helpers\PaypalHelper::class];
    }
  }

一切正常.现在,我希望能够修改控制器以采用PayPal界面.然后,我将更新我的服务提供者,以使用APP_ENV变量来确定要使用哪一个,从而有条件地传递真实类或模拟类以进行测试.我将一些调试器放入服务提供程序类中,但无法将其投入使用.我认为也许它只是按需加载它们,因此我在控制器内放置了一个断点.该类确实解决了,但它仍然从未进入服务提供者类!有人可以向我解释为什么会这样吗?即使我修改了代码以传递不同的类类型,它也没有出现.

Everything works as expected. Now I wanted to be able to modify controller to take a PayPal interface. I would then update my service provider to conditionally pass in either the real class or a mock one for testing, using the APP_ENV variable to determine which one to use. I put some debuggers into the service provider class and could not get it to ever go in. I thought perhaps that it only loads them on need, so I put a breakpoint inside my controller. The class did resolve, but it still never went into the service provider class! Can someone explain to me why this is the case? Even when I modified the code to pass in a different class type it did not pick up.

这是我调试此代码时看到的代码流: ControllerDispatcher-> resolveClassMethodDependencies-> resolveMethodDependencies-> transformDependency.此时,我们在RouteDependencyResolveerTrait中具有以下laravel代码:

Here is the code flow I see when I debug this: ControllerDispatcher -> resolveClassMethodDependencies -> resolveMethodDependencies -> transformDependency. At this point we have the following laravel code in the RouteDependencyResolveerTrait:

 protected function transformDependency(ReflectionParameter $parameter, $parameters, $originalParameters)
{
    $class = $parameter->getClass();

    // If the parameter has a type-hinted class, we will check to see if it is already in
    // the list of parameters. If it is we will just skip it as it is probably a model
    // binding and we do not want to mess with those; otherwise, we resolve it here.
    if ($class && ! $this->alreadyInParameters($class->name, $parameters)) {
        return $this->container->make($class->name);
    }
}

由于getClass()始终解析为接口名称,因此当我们调用container-> make()时,它总是失败并显示

Since getClass() always resolves to the interface name, when we call container->make(), it always fails with

Target [App\Helpers\PaypalHelperInterface] is not instantiable.

推荐答案

我终于找到了问题所在.我的问题是我的提供者根本没有被接听.这是解决方案

I finally found out the issue. My problem was that my provider wasn't being picked up at all. Here was the solution

composer dumpautoload
php artisan cache:clear

https://laravel.io /forum/02-08-2015-laravel-5-service-provider-not-working

这篇关于如何在Laravel容器中换出依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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