Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
a578938d
Commit
a578938d
authored
Apr 24, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added beforeAction and afterAction to Module.
parent
15b9f97c
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
46 additions
and
9 deletions
+46
-9
Controller.php
framework/base/Controller.php
+14
-8
Module.php
framework/base/Module.php
+32
-1
No files found.
framework/base/Controller.php
View file @
a578938d
...
...
@@ -19,7 +19,14 @@ use yii\helpers\StringHelper;
*/
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'
;
/**
* @event ActionEvent an event raised right after executing a controller action.
*/
const
EVENT_AFTER_ACTION
=
'afterAction'
;
/**
...
...
@@ -105,16 +112,15 @@ class Controller extends Component
if
(
$action
!==
null
)
{
$oldAction
=
$this
->
action
;
$this
->
action
=
$action
;
if
(
$this
->
beforeAction
(
$action
))
{
$status
=
$action
->
runWithParams
(
$params
);
$this
->
afterAction
(
$action
);
}
else
{
$status
=
1
;
$status
=
1
;
if
(
$this
->
module
->
beforeAction
(
$action
))
{
if
(
$this
->
beforeAction
(
$action
))
{
$status
=
$action
->
runWithParams
(
$params
);
$this
->
afterAction
(
$action
);
}
$this
->
module
->
afterAction
(
$action
);
}
$this
->
action
=
$oldAction
;
return
$status
;
}
else
{
throw
new
InvalidRouteException
(
'Unable to resolve the request: '
.
$this
->
getUniqueId
()
.
'/'
.
$id
);
...
...
framework/base/Module.php
View file @
a578938d
...
...
@@ -9,7 +9,6 @@ namespace yii\base;
use
Yii
;
use
yii\helpers\StringHelper
;
use
yii\helpers\FileHelper
;
/**
* Module is the base class for module and application classes.
...
...
@@ -39,6 +38,15 @@ use yii\helpers\FileHelper;
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).
*/
public
$params
=
array
();
...
...
@@ -613,4 +621,27 @@ abstract class Module extends Component
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
));
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment