[WebBundle] added support for URI in actions helper

This commit is contained in:
Fabien Potencier 2010-05-14 11:22:20 +02:00
parent b6852c3b6e
commit c8dde44b61

View File

@ -6,6 +6,7 @@ use Symfony\Components\Templating\Helper\Helper;
use Symfony\Components\DependencyInjection\ContainerInterface; use Symfony\Components\DependencyInjection\ContainerInterface;
use Symfony\Components\OutputEscaper\Escaper; use Symfony\Components\OutputEscaper\Escaper;
use Symfony\Components\HttpKernel\HttpKernelInterface; use Symfony\Components\HttpKernel\HttpKernelInterface;
use Symfony\Components\HttpKernel\Request;
/* /*
* This file is part of the Symfony framework. * This file is part of the Symfony framework.
@ -40,50 +41,56 @@ class ActionsHelper extends Helper
/** /**
* Outputs the Response content for a given controller. * Outputs the Response content for a given controller.
* *
* @param string $controller A controller name to execute (a string like BlogBundle:Post:index) * @param string $controller A controller name to execute (a string like BlogBundle:Post:index), or a relative URI
* @param array $path An array of path parameters
* @param array $query An array of query parameters
* @param array $options An array of options * @param array $options An array of options
* *
* @see render() * @see render()
*/ */
public function output($controller, array $path = array(), array $query = array(), array $options = array()) public function output($controller, array $options = array())
{ {
echo $this->render($controller, $path, $query, $options); echo $this->render($controller, $options);
} }
/** /**
* Returns the Response content for a given controller. * Returns the Response content for a given controller or URI.
* *
* Available options: * Available options:
* *
* * path: An array of path parameters (only when the first argument is a controller)
* * query: An array of query parameters (only when the first argument is a controller)
* * ignore_errors: true to return an empty string in case of an error * * ignore_errors: true to return an empty string in case of an error
* * alt: an alternative controller to execute in case of an error (an array with the controller, the path arguments, the query arguments) * * alt: an alternative controller to execute in case of an error (an array with the controller, the path arguments, the query arguments)
* *
* @param string $controller A controller name to execute (a string like BlogBundle:Post:index) * @param string $controller A controller name to execute (a string like BlogBundle:Post:index), or a relative URI
* @param array $path An array of path parameters
* @param array $query An array of query parameters
* @param array $options An array of options * @param array $options An array of options
*/ */
public function render($controller, array $path = array(), array $query = array(), array $options = array()) public function render($controller, array $options = array())
{ {
$options = array_merge(array( $options = array_merge(array(
'path' => array(),
'query' => array(),
'ignore_errors' => true, 'ignore_errors' => true,
'alt' => array(), 'alt' => array(),
), $options); ), $options);
if (!is_array($options['alt'])) if (!is_array($options['alt'])) {
{
$options['alt'] = array($options['alt']); $options['alt'] = array($options['alt']);
} }
$path = Escaper::unescape($path); $options['path'] = Escaper::unescape($options['path']);
$query = Escaper::unescape($query); $options['query'] = Escaper::unescape($options['query']);
// controller or URI?
$request = $this->container->getRequestService(); $request = $this->container->getRequestService();
$path['_controller'] = $controller; if (0 === strpos($controller, '/')) {
$path['_format'] = $request->getRequestFormat(); // URI
$subRequest = $request->duplicate($query, null, $path); $subRequest = Request::create($controller, 'get', array(), $request->cookies->all(), array(), $request->server->all());
} else {
// controller
$options['path']['_controller'] = $controller;
$options['path']['_format'] = $request->getRequestFormat();
$subRequest = $request->duplicate($options['query'], null, $options['path']);
}
try { try {
return $this->container->getKernelService()->handle($subRequest, HttpKernelInterface::EMBEDDED_REQUEST, true); return $this->container->getKernelService()->handle($subRequest, HttpKernelInterface::EMBEDDED_REQUEST, true);
@ -91,8 +98,10 @@ class ActionsHelper extends Helper
if ($options['alt']) { if ($options['alt']) {
$alt = $options['alt']; $alt = $options['alt'];
unset($options['alt']); unset($options['alt']);
$options['path'] = isset($alt[1]) ? $alt[1] : array();
$options['query'] = isset($alt[2]) ? $alt[2] : array();
return $this->render($alt[0], isset($alt[1]) ? $alt[1] : array(), isset($alt[2]) ? $alt[2] : array(), $options); return $this->render($alt[0], $options);
} }
if (!$options['ignore_errors']) { if (!$options['ignore_errors']) {