我觉得很蠢,但我不知道如何调用一个以接口为参数的函数.所以我在课堂上添加了一个方法:
public function sendSpool($messages = 10, KernelInterface $kernel)
{
$application = new Application($kernel);
$application->setAutoExit(false);
$input = new ArrayInput(array(
'command' => 'swiftmailer:spool:send',));
$output = new BufferedOutput();
$application->run($input, $output);
$content = $output->fetch();
return new Response($content);
}
Run Code Online (Sandbox Code Playgroud)
我怎么从我的控制器调用它?
我试过了:
$this->sendSpool('test', KernelInterface::class);
Run Code Online (Sandbox Code Playgroud)
然后:
$kernel = new HttpKernel();
$this->sendSpool('test', $kernel );
Run Code Online (Sandbox Code Playgroud)
这个内核接口在我的use语句中
use Symfony\Component\HttpKernel\KernelInterface;
Run Code Online (Sandbox Code Playgroud)
,但我不知道如何通过它,如果有人有几分钟,请帮我解释一下.谢谢.
您需要使用实现该接口的类的实例.基本上,它要求为您提供一个具有接口中描述的可用功能的类.
class MyKernel implements KernelInterface { /* functions here */ }
class x {
function x() {
$obj = new MyKernel();
$this->sendSpool('test', $obj);
}
}
Run Code Online (Sandbox Code Playgroud)
在Symfony中,通常有一种获取内核的方法.就像是:
$this->getApplication()->getKernel()
Run Code Online (Sandbox Code Playgroud)
但这取决于你的用例.
此外,您需要在实现接口的类中使用use语句,在您使用它的地方不需要它.
| 归档时间: |
|
| 查看次数: |
115 次 |
| 最近记录: |