我有这个密码
控制器
<?php
namespace App\Exchange\Helpers;
use App\Contracts\Exchange\Notification;
class Locker
{
protected $notification;
public function __construct(Notification $notification)
{
$this->notification = $notification;
}
public function index()
{
return $this->notification->sendMessage('test');
}接口
<?php
namespace App\Contracts\Exchange;
interface Notification
{
public function sendMessage($message);
}文件Kernel.php
namespace App\Providers;
use App\Contracts\Exchange\Notification;
use App\Exchange\Helpers\Notification\Telegram;
use Illuminate\Http\Request;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->bind(Notification::class, function (){
return new Telegram(env('TELEGRAM_EXCHANGE_TOKEN'), env('TELEGRAM_EXCHANGE_CHAT_ID'));
});
}如果我尝试使用新的Locker();,就会得到一个TypeError错误:对于函数App\Exchange\Helpers\Locker::__construct()来说,参数太少了,0在第1行的Psy Shell代码中传递,而确切地说是1期望的。
发布于 2022-07-22 10:11:01
您的控制器应该扩展Illuminate\Routing\Controller,以便依赖注入工作。或者使用__construct助手重构您的app方法:
<?php
namespace App\Exchange\Helpers;
use App\Contracts\Exchange\Notification;
class Locker
{
protected $notification;
public function __construct()
{
$this->notification = app(Notification::class);
}
public function index()
{
return $this->notification->sendMessage('test');
}
}https://stackoverflow.com/questions/73077259
复制相似问题