Question

I want to use the forward() method inside of a service. I defined http_kernel as argument for my service but I get this error :

FatalErrorException: Error: Call to undefined method forward()

config.yml :

 my.service:
     class: MyProject\MyBundle\MyService
     arguments: 
        http_kernel: "@http_kernel"

MyService.php :

public function __construct($http_kernel) {
    $this->http_kernel = $http_kernel;
    $response = $this->http_kernel->forward('AcmeHelloBundle:Hello:fancy', array(
        'name'  => $name,
         'color' => 'green',
    ));
}
Was it helpful?

Solution

The Symfony\Component\HttpKernel\HttpKernel object has no method forward. It's a method of Symfony\Bundle\FrameworkBundle\Controller\Controller
This is why you are getting this error.
As a side note, you should not do any computation into your constructor. Better create a process method which is called immediatly after.

Here's another way to do this:

services.yml

services:
    my.service:
        class: MyProject\MyBundle\MyService
        scope: request
        arguments:
            - @http_kernel
            - @request
        calls:
            - [ handleForward, [] ]

Note: scope: request is a mandatory parameter in order to give the @request service to your object.

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);
    }
}

OTHER TIPS

You can do this by injecting container into your service and then add forward function like below.

services.yml

 my.service:
     class: MyProject\MyBundle\MyService
     arguments: ['@service_container']

MyService.php

use Symfony\Component\DependencyInjection\ContainerInterface as Container;
use Symfony\Component\HttpKernel\HttpKernelInterface;

class MyService
{
    private $container;

    function __construct(Container $container)
    {
        $this->container = $container;     
    }

    public function forward($controller, array $path = array(), array $query = array())
    {
        $path['_controller'] = $controller;
        $subRequest = $this->container->get('request_stack')->getCurrentRequest()->duplicate($query, null, $path);

        return $this->container->get('http_kernel')->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
    }

    function yourFunction(){
        $response = $this->forward('AcmeHelloBundle:Hello:fancy', array(
            'name'  => $name,
            'color' => 'green',
        ));
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top