[英]Laravel facade type error without dependency injection
I am trying to access a function from anywhere in my app. 我正在尝试从应用程序中的任何位置访问功能。 But I get the following error:
但是我收到以下错误:
Type error: Argument 1 passed to App\\Helpers\\AwsAutoscale::__construct() must be an instance of Aws\\AutoScaling\\AutoScalingClient, none given, called in /home/vagrant/Site/app/Providers/AwsAutoscaleProvider.php on line 31
类型错误:传递给App \\ Helpers \\ AwsAutoscale :: __ construct()的参数1必须是Aws \\ AutoScaling \\ AutoScalingClient的实例,没有给出,在第31行的/home/vagrant/Site/app/Providers/AwsAutoscaleProvider.php中调用
My code is: 我的代码是:
App/Helpers/AwsAutoscale.php 应用程序/助手/ AwsAutoscale.php
<?php
namespace App\Helpers;
use Aws\AutoScaling\AutoScalingClient;
class AwsAutoscale
{
private $awsClient;
public function __construct(AutoScalingClient $awsClient)
{
$this->awsClient = $awsClient;
}
public function groupStats()
{
$result = $this->awsClient->describeAutoScalingGroups();
return collect([
'desired' => $result['AutoScalingGroups'][0]['DesiredCapacity'],
'min' => $result['AutoScalingGroups'][0]['MinSize'],
'max' => $result['AutoScalingGroups'][0]['MaxSize'],
'current' => count($result['AutoScalingGroups'][0]['Instances'])
]);
}
}
App/Providers/AutoScalingClientProvider.php 应用/供应商/ AutoScalingClientProvider.php
<?php
namespace App\Providers;
use Aws\AutoScaling\AutoScalingClient;
use Illuminate\Support\ServiceProvider;
class AutoScalingClientProvider extends ServiceProvider
{
/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->app->bind(AutoScalingClient::class, function () {
return new AutoScalingClient([
'AutoScalingGroupName' => config('aws.auto_scaling_client.name'),
'region' => config('aws.auto_scaling_client.region'),
'version' => config('aws.auto_scaling_client.version')
]);
});
}
}
App/Providers/AwsAutoScaleProvider.php 应用/供应商/ AwsAutoScaleProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AwsAutoscaleProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
//
$this->app->bind('autoscale', function()
{
return new \App\Helpers\AwsAutoscale;
});
}
}
config/app.php: 配置/ app.php:
'providers' => [
...
App\Providers\AwsAutoscaleProvider::class
...
'aliases' => [
...
'autoscale'=> App\Facades\AutoscaleFacade::class,
...
Code in controller: 控制器中的代码:
return autoscale::groupStats();
Calling this results in the error: 调用会导致错误:
Type error: Argument 1 passed to App\\Helpers\\AwsAutoscale::__construct() must be an instance of Aws\\AutoScaling\\AutoScalingClient, none given, called in /home/vagrant/Site/app/Providers/AwsAutoscaleProvider.php on line 31
类型错误:传递给App \\ Helpers \\ AwsAutoscale :: __ construct()的参数1必须是Aws \\ AutoScaling \\ AutoScalingClient的实例,没有给出,在第31行的/home/vagrant/Site/app/Providers/AwsAutoscaleProvider.php中调用
Let say you have this AwsAutoscale
helper saved within App/Helpers/AwsAutoscale.php
: 假设您已经将这个
AwsAutoscale
帮助程序保存在App/Helpers/AwsAutoscale.php
:
<?php
namespace App\Helpers;
use Aws\AutoScaling\AutoScalingClient;
class AwsAutoscale
{
private $awsClient;
public function __construct(AutoScalingClient $awsClient)
{
$this->awsClient = $awsClient;
}
public function groupStats()
{
$result = $this->awsClient->describeAutoScalingGroups();
return collect([
'desired' => $result['AutoScalingGroups'][0]['DesiredCapacity'],
'min' => $result['AutoScalingGroups'][0]['MinSize'],
'max' => $result['AutoScalingGroups'][0]['MaxSize'],
'current' => count($result['AutoScalingGroups'][0]['Instances']),
]);
}
}
And you want this AwsAutoscale
class instance to be accessible through Facade . 并且您希望可以通过Facade访问此
AwsAutoscale
类实例。 The first step is to register it within the service provider . 第一步是在服务提供商中注册它。 Let's put it together within the
App/Providers/AutoScalingClientProvider.php
file: 让我们将其放到
App/Providers/AutoScalingClientProvider.php
文件中:
<?php
namespace App\Providers;
use App\Helpers\AwsAutoscale;
use Aws\AutoScaling\AutoScalingClient;
use Illuminate\Support\ServiceProvider;
class AutoScalingClientProvider extends ServiceProvider
{
public function register()
{
// Bind the AutoScalingClient.
$this->app->bind(AutoScalingClient::class, function () {
return new AutoScalingClient([
'AutoScalingGroupName' => config('aws.auto_scaling_client.name'),
'region' => config('aws.auto_scaling_client.region'),
'version' => config('aws.auto_scaling_client.version')
]);
});
// Bind the AwsAutoscale helper.
$this->app->singleton('autoscale', function ($app) {
// You need to manually inject the dependency, which is the AutoScalingClient instance
return new AwsAutoscale($app->make(AutoScalingClient::class));
});
}
}
You can delete the App/Providers/AwsAutoScaleProvider.php
file, since we already bind the AwsAutoscale
on AutoScalingClientProvider.php
. 您可以删除
App/Providers/AwsAutoScaleProvider.php
文件,因为我们已经在AutoScalingClientProvider.php
上绑定了AwsAutoscale
。
If you want to access the AwsAutoscale
through facade, you'll need to create a facade class for this. 如果要通过Facade访问
AwsAutoscale
,则需要为此创建一个Facade类。 Let's say we put it on App\\Facades\\AutoscaleFacade.php
: 假设我们将其放在
App\\Facades\\AutoscaleFacade.php
:
<?php
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class AutoscaleFacade extends Facade
{
protected static function getFacadeAccessor()
{
// Return the key we use on service provider to bind AwsAutoclass instance
return 'autoscale';
}
}
Next, we need to register both the service provider and the facade class on the config\\app.php
: 接下来,我们需要在
config\\app.php
上注册服务提供商和Facade类:
<?php
return [
...
'providers' => [
...
App\Providers\AutoScalingClientProvider::class,
],
'aliases' => [
...
'Autoscale' => App\Facades\AutoscaleFacade::class,
],
];
Now you can easily access the AwsAutoscale
instance using the registered alias like so: 现在,您可以使用已注册的别名轻松访问
AwsAutoscale
实例,如下所示:
Route::get('/foo', function () {
// Access it with the Autoscale facade class not AwsAutoscale
dd(\Autoscale::groupStats());
});
Hope this gives you some ideas. 希望这能给您一些想法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.