浏览代码

[WebBundle] added support for URI in actions helper

Fabien Potencier 15 年之前
父节点
当前提交
c8dde44b61
共有 1 个文件被更改,包括 27 次插入18 次删除
  1. 27 18
      src/Symfony/Framework/WebBundle/Helper/ActionsHelper.php

+ 27 - 18
src/Symfony/Framework/WebBundle/Helper/ActionsHelper.php

@@ -6,6 +6,7 @@ use Symfony\Components\Templating\Helper\Helper;
 use Symfony\Components\DependencyInjection\ContainerInterface;
 use Symfony\Components\OutputEscaper\Escaper;
 use Symfony\Components\HttpKernel\HttpKernelInterface;
+use Symfony\Components\HttpKernel\Request;
 
 /*
  * This file is part of the Symfony framework.
@@ -40,50 +41,56 @@ class ActionsHelper extends Helper
     /**
      * Outputs the Response content for a given controller.
      *
-     * @param string $controller A controller name to execute (a string like BlogBundle:Post:index)
-     * @param array  $path       An array of path parameters
-     * @param array  $query      An array of query parameters
+     * @param string $controller A controller name to execute (a string like BlogBundle:Post:index), or a relative URI
      * @param array  $options    An array of options
      *
      * @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:
      *
+     *  * 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
      *  * 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 array  $path       An array of path parameters
-     * @param array  $query      An array of query parameters
+     * @param string $controller A controller name to execute (a string like BlogBundle:Post:index), or a relative URI
      * @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(
+            'path'          => array(),
+            'query'         => array(),
             'ignore_errors' => true,
             'alt'           => array(),
         ), $options);
 
-        if (!is_array($options['alt']))
-        {
+        if (!is_array($options['alt'])) {
             $options['alt'] = array($options['alt']);
         }
 
-        $path = Escaper::unescape($path);
-        $query = Escaper::unescape($query);
+        $options['path'] = Escaper::unescape($options['path']);
+        $options['query'] = Escaper::unescape($options['query']);
 
+        // controller or URI?
         $request = $this->container->getRequestService();
-        $path['_controller'] = $controller;
-        $path['_format'] = $request->getRequestFormat();
-        $subRequest = $request->duplicate($query, null, $path);
+        if (0 === strpos($controller, '/')) {
+            // URI
+            $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 {
             return $this->container->getKernelService()->handle($subRequest, HttpKernelInterface::EMBEDDED_REQUEST, true);
@@ -91,8 +98,10 @@ class ActionsHelper extends Helper
             if ($options['alt']) {
                 $alt = $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']) {