Symfony\Bundle\FrameworkBundle\Controller\Controller::forward PHP Method

forward() protected method

Forwards the request to another controller.
protected forward ( string $controller, array $path = [], array $query = [] ) : Response
$controller string The controller name (a string like BlogBundle:Post:index)
$path array An array of path parameters
$query array An array of query parameters
return Symfony\Component\HttpFoundation\Response A Response instance
    protected function forward($controller, array $path = array(), array $query = array())
    {
        $request = $this->container->get('request_stack')->getCurrentRequest();
        $path['_forwarded'] = $request->attributes;
        $path['_controller'] = $controller;
        $subRequest = $request->duplicate($query, null, $path);
        return $this->container->get('http_kernel')->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
    }

Usage Example

Esempio n. 1
0
 public function testForward()
 {
     $request = Request::create('/');
     $request->setLocale('fr');
     $request->setRequestFormat('xml');
     $kernel = $this->getMock('Symfony\\Component\\HttpKernel\\HttpKernelInterface');
     $kernel->expects($this->once())->method('handle')->will($this->returnCallback(function (Request $request) {
         return new Response($request->getRequestFormat() . '--' . $request->getLocale());
     }));
     $container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
     $container->expects($this->at(0))->method('get')->will($this->returnValue($request));
     $container->expects($this->at(1))->method('get')->will($this->returnValue($kernel));
     $controller = new Controller();
     $controller->setContainer($container);
     $response = $controller->forward('a_controller');
     $this->assertEquals('xml--fr', $response->getContent());
 }
All Usage Examples Of Symfony\Bundle\FrameworkBundle\Controller\Controller::forward