将SoapClient注入到一个类中(依赖注入)

时间:2019-01-25 17:17:25

标签: php laravel dependency-injection

我试图将SoapClient依赖项注入Foo类,我已经创建了一个提供程序容器,但无法解析该依赖项。

这是我要在其中注入依赖项的类。

   namespace App\Services
   Class Foo{

      public function __construct(   )
      {


          $this->SoapClient = resolve('MySoapClient');

      }

      public  function someMethod($data)
      {


          $req=$this->SoapClient->getSomething($params);
          return $req;
      }

  }

这是服务提供商

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Broadcast;
use \Illuminate\Foundation\Application;

class AppServiceProvider extends ServiceProvider
{


    public function register()
    {


        $this->app->bind('MySoapClient', function ($app) {
        return new \SoapClient(env('wsdl'));
        });

     }

}

我只想将SoapClient注入Foo类。上面的代码不起作用,我得到了(类MySoapClient不存在)

0 个答案:

没有答案