Commit d1611e66 by Qiang Xue

...

parent 1db738ef
......@@ -15,10 +15,10 @@ namespace yii\base;
* Action provides a way to divide a complex controller into
* smaller actions in separate class files.
*
* Derived classes must implement {@link run()} which is invoked by
* controller when the action is requested.
*
* An action instance can access its controller via {@link getController controller} property.
* Derived classes must implement a method named `run()`. This method
* will be invoked by the controller when the action is requested.
* The `run()` method can have parameters which will be filled up
* automatically according to their names.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
......@@ -35,17 +35,21 @@ class Action extends Component
public $controller;
/**
* Extracts the input parameters according to the signature of the "run()" method.
* This method is invoked by controller when it attempts to run the action
* with the user supplied parameters.
* @param array $params the parameters in name-value pairs
* @return array|boolean the extracted parameters in the order as declared in the "run()" method.
* False is returned if the input parameters do not follow the method declaration.
* Runs the action with the supplied parameters.
* This method is invoked by the controller.
* @param array $params the input parameters in terms of name-value pairs.
* @return boolean whether the input parameters are valid
*/
public function normalizeParams($params)
public function runWithParams($params)
{
$method = new \ReflectionMethod($this, 'run');
return $this->normalizeParamsByMethod($method, $params);
$params = $this->normalizeParamsByMethod($method, $params);
if ($params !== false) {
call_user_func_array(array($this, 'run'), $params);
return true;
} else {
return false;
}
}
/**
......
<?php
/**
* CFilter class file.
* ActionFilter class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\base;
/**
* CFilter is the base class for all filters.
* ActionFilter is the base class for all filters.
*
* A filter can be applied before and after an action is executed.
* It can modify the context that the action is to run or decorate the result that the
......@@ -19,11 +20,9 @@
* before the action, and {@link postFilter()} for filtering logic after the action.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.web.filters
* @since 1.0
* @since 2.0
*/
class CFilter extends CComponent implements IFilter
class ActionFilter extends CComponent implements IFilter
{
/**
* Performs the filtering.
......@@ -32,7 +31,7 @@ class CFilter extends CComponent implements IFilter
* child classes. If a child class needs to override this method,
* make sure it calls <code>$filterChain->run()</code>
* if the action should be executed.
* @param CFilterChain $filterChain the filter chain that the filter is on.
* @param ActionFilterChain $filterChain the filter chain that the filter is on.
*/
public function filter($filterChain)
{
......@@ -56,7 +55,7 @@ class CFilter extends CComponent implements IFilter
/**
* Performs the pre-action filtering.
* @param CFilterChain $filterChain the filter chain that the filter is on.
* @param ActionFilterChain $filterChain the filter chain that the filter is on.
* @return boolean whether the filtering process should continue and the action
* should be executed.
*/
......@@ -67,7 +66,7 @@ class CFilter extends CComponent implements IFilter
/**
* Performs the post-action filtering.
* @param CFilterChain $filterChain the filter chain that the filter is on.
* @param ActionFilterChain $filterChain the filter chain that the filter is on.
*/
protected function postFilter($filterChain)
{
......
......@@ -252,12 +252,10 @@ abstract class Controller extends Component implements Initable
$priorAction = $this->_action;
$this->_action = $action;
if ($this->beforeAction($action)) {
$params = $action->normalizeParams($this->getActionParams());
if ($params === false) {
$this->invalidActionParams($action);
} else {
call_user_func_array(array($action, 'run'), $params);
if ($action->runWithParams($this->getActionParams())) {
$this->afterAction($action);
} else {
$this->invalidActionParams($action);
}
}
$this->_action = $priorAction;
......
......@@ -12,7 +12,8 @@ namespace yii\base;
/**
* InlineAction represents an action that is defined as a controller method.
*
* The method name is like 'actionXYZ' where 'XYZ' stands for the action name.
* The name of the controller method should be in the format of `actionXyz`
* where `Xyz` stands for the action ID (e.g. `actionIndex`).
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
......@@ -20,29 +21,18 @@ namespace yii\base;
class InlineAction extends Action
{
/**
* Runs the action.
* This method is invoked by the controller to run the action.
* @param array $params the input parameters
* Runs the action with the supplied parameters.
* This method is invoked by the controller.
* @param array $params the input parameters in terms of name-value pairs.
* @return boolean whether the input parameters are valid
*/
public function run($params)
{
call_user_func_array(array($this->controller, 'action' . $this->id), $params);
}
/**
* Extracts the input parameters according to the signature of the controller action method.
* This method is invoked by controller when it attempts to run the action
* with the user supplied parameters.
* @param array $params the parameters in name-value pairs
* @return array|boolean the extracted parameters in the order as declared in the controller action method.
* False is returned if the input parameters do not follow the method declaration.
*/
public function normalizeParams($params)
public function runWithParams($params)
{
$method = new \ReflectionMethod($this->controller, 'action' . $this->id);
$params = $this->normalizeParams($method, $params);
$params = $this->normalizeParamsByMethod($method, $params);
if ($params !== false) {
return array($params);
call_user_func_array(array($this->controller, 'action' . $this->id), $params);
return true;
} else {
return false;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment