ソースを参照

ZF-4026:
- Added clearParams() method to abstract request
- Added flag to ActionStack to allow clearing params prior to calling new action

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@18174 44c647ce-9c0f-0410-b52a-842ac1e357ba

matthew 16 年 前
コミット
af1c8884a7

+ 40 - 5
library/Zend/Controller/Plugin/ActionStack.php

@@ -59,6 +59,14 @@ class Zend_Controller_Plugin_ActionStack extends Zend_Controller_Plugin_Abstract
     );
 
     /**
+     * Flag to determine whether request parameters are cleared between actions, or whether new parameters
+     * are added to existing request parameters.
+     *
+     * @var Bool
+     */
+    protected $_clearRequestParams = false;
+ 
+    /**
      * Constructor
      *
      * @param  Zend_Registry $registry
@@ -126,6 +134,28 @@ class Zend_Controller_Plugin_ActionStack extends Zend_Controller_Plugin_Abstract
     }
 
     /**
+     *  Set clearRequestParams flag
+     *
+     *  @param  bool $clearRequestParams
+     *  @return Zend_Controller_Plugin_ActionStack
+     */
+    public function setClearRequestParams($clearRequestParams)
+    {
+        $this->_clearRequestParams = (bool) $clearRequestParams;
+        return $this;
+    }
+ 
+    /**
+     * Retrieve clearRequestParams flag
+     *
+     * @return bool
+     */
+    public function getClearRequestParams()
+    {
+        return $this->_clearRequestParams;
+    }
+ 
+    /**
      * Retrieve action stack
      *
      * @return array
@@ -236,10 +266,15 @@ class Zend_Controller_Plugin_ActionStack extends Zend_Controller_Plugin_Abstract
      */
     public function forward(Zend_Controller_Request_Abstract $next)
     {
-        $this->getRequest()->setModuleName($next->getModuleName())
-                           ->setControllerName($next->getControllerName())
-                           ->setActionName($next->getActionName())
-                           ->setParams($next->getParams())
-                           ->setDispatched(false);
+        $request = $this->getRequest();
+        if ($this->getClearRequestParams()) {
+            $request->clearParams();
+        }
+        
+        $request->setModuleName($next->getModuleName())
+                ->setControllerName($next->getControllerName())
+                ->setActionName($next->getActionName())
+                ->setParams($next->getParams())
+                ->setDispatched(false);
     }
 }

+ 11 - 0
library/Zend/Controller/Request/Abstract.php

@@ -322,6 +322,17 @@ abstract class Zend_Controller_Request_Abstract
     }
 
     /**
+     * Unset all user parameters
+     *
+     * @return Zend_Controller_Request_Abstract
+     */
+    public function clearParams()
+    {
+        $this->_params = array();
+        return $this;
+    }
+
+    /**
      * Set flag indicating whether or not request has been dispatched
      *
      * @param boolean $flag

+ 23 - 0
tests/Zend/Controller/Plugin/ActionStackTest.php

@@ -282,6 +282,29 @@ class Zend_Controller_Plugin_ActionStackTest extends PHPUnit_Framework_TestCase
         $this->assertFalse($request->isDispatched());
     }
 
+    public function testForwardResetsRequestParamsIfFlagSet()
+    {
+        $plugin   = new Zend_Controller_Plugin_ActionStack();
+        $request  = $this->getNewRequest();
+        $params   = array('foo' => 'bar','baz'=>'bat');
+        $request->setParams($params);
+        $plugin->setRequest($request);
+
+        $this->assertEquals($params,$plugin->getRequest()->getParams());
+
+        $next = $this->getNewRequest();
+        $plugin->forward($next);
+
+        $this->assertEquals($params,$plugin->getRequest()->getParams());
+
+        $plugin->setClearRequestParams(true);
+
+        $next = $this->getNewRequest();
+        $plugin->forward($next);
+
+        $this->assertEquals(array(),$plugin->getRequest()->getParams());
+    }
+
     /**
      * @return void
      */

+ 7 - 0
tests/Zend/Controller/Request/HttpTest.php

@@ -197,6 +197,13 @@ class Zend_Controller_Request_HttpTest extends PHPUnit_Framework_TestCase
         }
     }
 
+    public function testClearParams()
+    {
+        $this->_request->setParam('foo', 'bar');
+        $this->_request->clearParams();
+        $this->assertNull($this->_request->getParam('foo'));
+    }
+
     public function testSetGetParam()
     {
         $this->_request->setParam('foo', 'bar');