Commit a578938d by Qiang Xue

Added beforeAction and afterAction to Module.

parent 15b9f97c
...@@ -19,7 +19,14 @@ use yii\helpers\StringHelper; ...@@ -19,7 +19,14 @@ use yii\helpers\StringHelper;
*/ */
class Controller extends Component class Controller extends Component
{ {
/**
* @event ActionEvent an event raised right before executing a controller action.
* You may set [[ActionEvent::isValid]] to be false to cancel the action execution.
*/
const EVENT_BEFORE_ACTION = 'beforeAction'; const EVENT_BEFORE_ACTION = 'beforeAction';
/**
* @event ActionEvent an event raised right after executing a controller action.
*/
const EVENT_AFTER_ACTION = 'afterAction'; const EVENT_AFTER_ACTION = 'afterAction';
/** /**
...@@ -105,16 +112,15 @@ class Controller extends Component ...@@ -105,16 +112,15 @@ class Controller extends Component
if ($action !== null) { if ($action !== null) {
$oldAction = $this->action; $oldAction = $this->action;
$this->action = $action; $this->action = $action;
$status = 1;
if ($this->beforeAction($action)) { if ($this->module->beforeAction($action)) {
$status = $action->runWithParams($params); if ($this->beforeAction($action)) {
$this->afterAction($action); $status = $action->runWithParams($params);
} else { $this->afterAction($action);
$status = 1; }
$this->module->afterAction($action);
} }
$this->action = $oldAction; $this->action = $oldAction;
return $status; return $status;
} else { } else {
throw new InvalidRouteException('Unable to resolve the request: ' . $this->getUniqueId() . '/' . $id); throw new InvalidRouteException('Unable to resolve the request: ' . $this->getUniqueId() . '/' . $id);
......
...@@ -9,7 +9,6 @@ namespace yii\base; ...@@ -9,7 +9,6 @@ namespace yii\base;
use Yii; use Yii;
use yii\helpers\StringHelper; use yii\helpers\StringHelper;
use yii\helpers\FileHelper;
/** /**
* Module is the base class for module and application classes. * Module is the base class for module and application classes.
...@@ -39,6 +38,15 @@ use yii\helpers\FileHelper; ...@@ -39,6 +38,15 @@ use yii\helpers\FileHelper;
abstract class Module extends Component abstract class Module extends Component
{ {
/** /**
* @event ActionEvent an event raised before executing a controller action.
* You may set [[ActionEvent::isValid]] to be false to cancel the action execution.
*/
const EVENT_BEFORE_ACTION = 'beforeAction';
/**
* @event ActionEvent an event raised after executing a controller action.
*/
const EVENT_AFTER_ACTION = 'afterAction';
/**
* @var array custom module parameters (name => value). * @var array custom module parameters (name => value).
*/ */
public $params = array(); public $params = array();
...@@ -613,4 +621,27 @@ abstract class Module extends Component ...@@ -613,4 +621,27 @@ abstract class Module extends Component
return isset($controller) ? array($controller, $route) : false; return isset($controller) ? array($controller, $route) : false;
} }
/**
* This method is invoked right before an action is to be executed (after all possible filters.)
* You may override this method to do last-minute preparation for the action.
* @param Action $action the action to be executed.
* @return boolean whether the action should continue to be executed.
*/
public function beforeAction($action)
{
$event = new ActionEvent($action);
$this->trigger(self::EVENT_BEFORE_ACTION, $event);
return $event->isValid;
}
/**
* This method is invoked right after an action is executed.
* You may override this method to do some postprocessing for the action.
* @param Action $action the action just executed.
*/
public function afterAction($action)
{
$this->trigger(self::EVENT_AFTER_ACTION, new ActionEvent($action));
}
} }
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