该Symfony\Component\HttpKernel\HttpKernel
对象没有方法forward
。这是一种方法,
这就是您收到此错误的原因。
作为旁注,您不应该对构造函数进行任何计算。最好创建一个在之后立即调用的方法。Symfony\Bundle\FrameworkBundle\Controller\Controller
process
这是另一种方法:
services.yml
services:
my.service:
class: MyProject\MyBundle\MyService
scope: request
arguments:
- @http_kernel
- @request
calls:
- [ handleForward, [] ]
注意:scope: request
是一个强制参数,以便为@request
您的对象提供服务。
MyProject\MyBundle\MyService
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpFoundation\Request;
class MyService
{
protected $request;
protected $kernel;
public function __construct(HttpKernelInterface $kernel, Request $request)
{
$this->kernel = $kernel;
$this->request = $request;
}
public function handleForward()
{
$controller = 'AcmeHelloBundle:Hello:fancy';
$path = array(
'name' => $name,
'color' => 'green',
'_controller' => $controller
);
$subRequest = $this->request->duplicate(array(), null, $path);
$response = $this->kernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
}
}