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
9d7597d6
Commit
9d7597d6
authored
Jun 16, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #548 from cebe/action-response
implementation of Response tied to actions
parents
6b6390f7
71782b34
Hide whitespace changes
Inline
Side-by-side
Showing
18 changed files
with
102 additions
and
89 deletions
+102
-89
SiteController.php
apps/advanced/backend/controllers/SiteController.php
+2
-2
SiteController.php
apps/advanced/frontend/controllers/SiteController.php
+3
-3
SiteController.php
apps/basic/controllers/SiteController.php
+3
-3
Action.php
framework/yii/base/Action.php
+27
-0
Application.php
framework/yii/base/Application.php
+0
-1
Controller.php
framework/yii/base/Controller.php
+20
-3
ErrorHandler.php
framework/yii/base/ErrorHandler.php
+8
-2
Module.php
framework/yii/base/Module.php
+3
-3
Application.php
framework/yii/console/Application.php
+2
-35
Controller.php
framework/yii/console/Controller.php
+10
-0
Application.php
framework/yii/web/Application.php
+1
-22
CaptchaAction.php
framework/yii/web/CaptchaAction.php
+1
-1
Controller.php
framework/yii/web/Controller.php
+11
-0
HttpCache.php
framework/yii/web/HttpCache.php
+5
-4
Request.php
framework/yii/web/Request.php
+1
-1
User.php
framework/yii/web/User.php
+4
-5
VerbFilter.php
framework/yii/web/VerbFilter.php
+1
-1
HtmlTest.php
tests/unit/framework/helpers/HtmlTest.php
+0
-3
No files found.
apps/advanced/backend/controllers/SiteController.php
View file @
9d7597d6
...
...
@@ -17,7 +17,7 @@ class SiteController extends Controller
{
$model
=
new
LoginForm
();
if
(
$this
->
populate
(
$_POST
,
$model
)
&&
$model
->
login
())
{
return
Yii
::
$app
->
response
->
redirect
(
array
(
'site/index'
));
return
$this
->
response
->
redirect
(
array
(
'site/index'
));
}
else
{
return
$this
->
render
(
'login'
,
array
(
'model'
=>
$model
,
...
...
@@ -28,6 +28,6 @@ class SiteController extends Controller
public
function
actionLogout
()
{
Yii
::
$app
->
user
->
logout
();
return
Yii
::
$app
->
response
->
redirect
(
array
(
'site/index'
));
return
$this
->
response
->
redirect
(
array
(
'site/index'
));
}
}
apps/advanced/frontend/controllers/SiteController.php
View file @
9d7597d6
...
...
@@ -27,7 +27,7 @@ class SiteController extends Controller
{
$model
=
new
LoginForm
();
if
(
$this
->
populate
(
$_POST
,
$model
)
&&
$model
->
login
())
{
return
Yii
::
$app
->
response
->
redirect
(
array
(
'site/index'
));
return
$this
->
response
->
redirect
(
array
(
'site/index'
));
}
else
{
return
$this
->
render
(
'login'
,
array
(
'model'
=>
$model
,
...
...
@@ -38,7 +38,7 @@ class SiteController extends Controller
public
function
actionLogout
()
{
Yii
::
$app
->
user
->
logout
();
return
Yii
::
$app
->
response
->
redirect
(
array
(
'site/index'
));
return
$this
->
response
->
redirect
(
array
(
'site/index'
));
}
public
function
actionContact
()
...
...
@@ -46,7 +46,7 @@ class SiteController extends Controller
$model
=
new
ContactForm
;
if
(
$this
->
populate
(
$_POST
,
$model
)
&&
$model
->
contact
(
Yii
::
$app
->
params
[
'adminEmail'
]))
{
Yii
::
$app
->
session
->
setFlash
(
'contactFormSubmitted'
);
return
Yii
::
$app
->
response
->
refresh
();
return
$this
->
response
->
refresh
();
}
else
{
return
$this
->
render
(
'contact'
,
array
(
'model'
=>
$model
,
...
...
apps/basic/controllers/SiteController.php
View file @
9d7597d6
...
...
@@ -27,7 +27,7 @@ class SiteController extends Controller
{
$model
=
new
LoginForm
();
if
(
$this
->
populate
(
$_POST
,
$model
)
&&
$model
->
login
())
{
return
Yii
::
$app
->
response
->
redirect
(
array
(
'site/index'
));
return
$this
->
response
->
redirect
(
array
(
'site/index'
));
}
else
{
return
$this
->
render
(
'login'
,
array
(
'model'
=>
$model
,
...
...
@@ -38,7 +38,7 @@ class SiteController extends Controller
public
function
actionLogout
()
{
Yii
::
$app
->
user
->
logout
();
return
Yii
::
$app
->
response
->
redirect
(
array
(
'site/index'
));
return
$this
->
response
->
redirect
(
array
(
'site/index'
));
}
public
function
actionContact
()
...
...
@@ -46,7 +46,7 @@ class SiteController extends Controller
$model
=
new
ContactForm
;
if
(
$this
->
populate
(
$_POST
,
$model
)
&&
$model
->
contact
(
Yii
::
$app
->
params
[
'adminEmail'
]))
{
Yii
::
$app
->
session
->
setFlash
(
'contactFormSubmitted'
);
return
Yii
::
$app
->
response
->
refresh
();
return
$this
->
response
->
refresh
();
}
else
{
return
$this
->
render
(
'contact'
,
array
(
'model'
=>
$model
,
...
...
framework/yii/base/Action.php
View file @
9d7597d6
...
...
@@ -7,6 +7,8 @@
namespace
yii\base
;
use
Yii
;
/**
* Action is the base class for all controller action classes.
*
...
...
@@ -39,6 +41,31 @@ class Action extends Component
* @var Controller the controller that owns this action
*/
public
$controller
;
/**
* @var Response
*/
private
$_response
;
/**
* @return Response|\yii\console\Response|\yii\web\Response
*/
public
function
getResponse
()
{
if
(
$this
->
_response
===
null
)
{
// TODO use applications response factory here
//$this->_response = new Response();
}
return
$this
->
_response
;
}
/**
* @param Response $response
*/
public
function
setResponse
(
$response
)
{
$this
->
_response
=
$response
;
}
/**
* Constructor.
...
...
framework/yii/base/Application.php
View file @
9d7597d6
...
...
@@ -155,7 +155,6 @@ abstract class Application extends Module
*/
abstract
public
function
handleRequest
(
$request
);
private
$_runtimePath
;
/**
...
...
framework/yii/base/Controller.php
View file @
9d7597d6
...
...
@@ -15,7 +15,7 @@ use Yii;
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class
Controller
extends
Component
abstract
class
Controller
extends
Component
{
/**
* @event ActionEvent an event raised right before executing a controller action.
...
...
@@ -110,22 +110,39 @@ class Controller extends Component
if
(
$action
!==
null
)
{
$oldAction
=
$this
->
action
;
$this
->
action
=
$action
;
$result
=
null
;
if
(
$this
->
module
->
beforeAction
(
$action
))
{
if
(
$this
->
beforeAction
(
$action
))
{
$result
=
$action
->
runWithParams
(
$params
);
if
(
$result
!==
null
)
{
$this
->
handleActionResult
(
$result
,
$action
);
}
$this
->
afterAction
(
$action
);
}
$this
->
module
->
afterAction
(
$action
);
}
$this
->
action
=
$oldAction
;
return
$
result
;
return
$
action
->
getResponse
()
;
}
else
{
throw
new
InvalidRouteException
(
'Unable to resolve the request: '
.
$this
->
getUniqueId
()
.
'/'
.
$id
);
}
}
/**
* Handles the return value of an action
* @param mixed $result
* @param Action $action
*/
protected
abstract
function
handleActionResult
(
&
$result
,
$action
);
/**
* @return Response the response object of the current action. null if no action is running
*/
public
function
getResponse
()
{
return
$this
->
action
!==
null
?
$this
->
action
->
getResponse
()
:
null
;
}
/**
* Runs a request specified in terms of a route.
* The route can be either an ID of an action within this controller or a complete route consisting
* of module IDs, controller ID and action ID. If the route starts with a slash '/', the parsing of
...
...
framework/yii/base/ErrorHandler.php
View file @
9d7597d6
...
...
@@ -9,6 +9,7 @@ namespace yii\base;
use
Yii
;
use
yii\web\HttpException
;
use
yii\web\Response
;
/**
* ErrorHandler handles uncaught PHP errors and exceptions.
...
...
@@ -89,8 +90,13 @@ class ErrorHandler extends Component
$useErrorView
=
!
YII_DEBUG
||
$exception
instanceof
UserException
;
$response
=
Yii
::
$app
->
getResponse
();
$response
->
getHeaders
()
->
removeAll
();
if
(
Yii
::
$app
->
controller
!==
null
)
{
$response
=
Yii
::
$app
->
controller
->
getResponse
();
$response
->
getHeaders
()
->
removeAll
();
}
if
(
empty
(
$response
))
{
$response
=
new
Response
();
}
if
(
$useErrorView
&&
$this
->
errorAction
!==
null
)
{
$result
=
Yii
::
$app
->
runAction
(
$this
->
errorAction
);
...
...
framework/yii/base/Module.php
View file @
9d7597d6
...
...
@@ -571,7 +571,7 @@ abstract class Module extends Component
* If the route is empty, the method will use [[defaultRoute]].
* @param string $route the route that specifies the action.
* @param array $params the parameters to be passed to the action
* @return
mixed the result
of the action.
* @return
Response the resulting response
of the action.
* @throws InvalidRouteException if the requested route cannot be resolved into an action successfully
*/
public
function
runAction
(
$route
,
$params
=
array
())
...
...
@@ -582,9 +582,9 @@ abstract class Module extends Component
list
(
$controller
,
$actionID
)
=
$parts
;
$oldController
=
Yii
::
$app
->
controller
;
Yii
::
$app
->
controller
=
$controller
;
$res
ult
=
$controller
->
runAction
(
$actionID
,
$params
);
$res
ponse
=
$controller
->
runAction
(
$actionID
,
$params
);
Yii
::
$app
->
controller
=
$oldController
;
return
$res
ult
;
return
$res
ponse
;
}
else
{
throw
new
InvalidRouteException
(
'Unable to resolve the request "'
.
trim
(
$this
->
getUniqueId
()
.
'/'
.
$route
,
'/'
)
.
'".'
);
}
...
...
framework/yii/console/Application.php
View file @
9d7597d6
...
...
@@ -88,43 +88,13 @@ class Application extends \yii\base\Application
* Handles the specified request.
* @param Request $request the request to be handled
* @return Response the resulting response
* @throws Exception if the route is invalid
*/
public
function
handleRequest
(
$request
)
{
list
(
$route
,
$params
)
=
$request
->
resolve
();
$result
=
$this
->
runAction
(
$route
,
$params
);
if
(
$result
instanceof
Response
)
{
return
$result
;
}
else
{
$response
=
$this
->
getResponse
();
$response
->
exitStatus
=
(
int
)
$result
;
return
$response
;
}
}
/**
* Returns the response component.
* @return Response the response component
*/
public
function
getResponse
()
{
return
$this
->
getComponent
(
'response'
);
}
/**
* Runs a controller action specified by a route.
* This method parses the specified route and creates the corresponding child module(s), controller and action
* instances. It then calls [[Controller::runAction()]] to run the action with the given parameters.
* If the route is empty, the method will use [[defaultRoute]].
* @param string $route the route that specifies the action.
* @param array $params the parameters to be passed to the action
* @return integer the status code returned by the action execution. 0 means normal, and other values mean abnormal.
* @throws Exception if the route is invalid
*/
public
function
runAction
(
$route
,
$params
=
array
())
{
try
{
return
parent
::
runAction
(
$route
,
$params
);
return
$this
->
runAction
(
$route
,
$params
);
}
catch
(
InvalidRouteException
$e
)
{
throw
new
Exception
(
\Yii
::
t
(
'yii'
,
'Unknown command "{command}".'
,
array
(
'{command}'
=>
$route
)),
0
,
$e
);
}
...
...
@@ -156,9 +126,6 @@ class Application extends \yii\base\Application
'request'
=>
array
(
'class'
=>
'yii\console\Request'
,
),
'response'
=>
array
(
'class'
=>
'yii\console\Response'
,
),
));
}
}
framework/yii/console/Controller.php
View file @
9d7597d6
...
...
@@ -135,6 +135,16 @@ class Controller extends \yii\base\Controller
}
/**
* Handles the return value of an action
* @param mixed $result
* @param Action $action
*/
protected
function
handleActionResult
(
&
$result
,
$action
)
{
$action
->
getResponse
()
->
exitStatus
=
(
int
)
$result
;
}
/**
* Formats a string with ANSI codes
*
* You may pass additional parameters using the constants defined in [[yii\helpers\base\Console]].
...
...
framework/yii/web/Application.php
View file @
9d7597d6
...
...
@@ -65,16 +65,7 @@ class Application extends \yii\base\Application
$params
=
array_splice
(
$this
->
catchAll
,
1
);
}
try
{
$result
=
$this
->
runAction
(
$route
,
$params
);
if
(
$result
instanceof
Response
)
{
return
$result
;
}
else
{
$response
=
$this
->
getResponse
();
if
(
$result
!==
null
)
{
$response
->
setContent
(
$result
);
}
return
$response
;
}
return
$this
->
runAction
(
$route
,
$params
);
}
catch
(
InvalidRouteException
$e
)
{
throw
new
HttpException
(
404
,
$e
->
getMessage
(),
$e
->
getCode
(),
$e
);
}
...
...
@@ -116,15 +107,6 @@ class Application extends \yii\base\Application
}
/**
* Returns the response component.
* @return Response the response component
*/
public
function
getResponse
()
{
return
$this
->
getComponent
(
'response'
);
}
/**
* Returns the session component.
* @return Session the session component
*/
...
...
@@ -162,9 +144,6 @@ class Application extends \yii\base\Application
'request'
=>
array
(
'class'
=>
'yii\web\Request'
,
),
'response'
=>
array
(
'class'
=>
'yii\web\Response'
,
),
'session'
=>
array
(
'class'
=>
'yii\web\Session'
,
),
...
...
framework/yii/web/CaptchaAction.php
View file @
9d7597d6
...
...
@@ -325,7 +325,7 @@ class CaptchaAction extends Action
*/
protected
function
setHttpHeaders
()
{
Yii
::
$app
->
getResponse
()
->
getHeaders
()
$this
->
getResponse
()
->
getHeaders
()
->
set
(
'Pragma'
,
'public'
)
->
set
(
'Expires'
,
'0'
)
->
set
(
'Cache-Control'
,
'must-revalidate, post-check=0, pre-check=0'
)
...
...
framework/yii/web/Controller.php
View file @
9d7597d6
...
...
@@ -8,6 +8,7 @@
namespace
yii\web
;
use
Yii
;
use
yii\base\Action
;
use
yii\base\InlineAction
;
/**
...
...
@@ -62,6 +63,16 @@ class Controller extends \yii\base\Controller
}
/**
* Handles the return value of an action
* @param mixed $result
* @param Action $action
*/
protected
function
handleActionResult
(
&
$result
,
$action
)
{
$action
->
getResponse
()
->
setContent
(
$result
);
}
/**
* Creates a URL using the given route and parameters.
*
* This method enhances [[UrlManager::createUrl()]] by supporting relative routes.
...
...
framework/yii/web/HttpCache.php
View file @
9d7597d6
...
...
@@ -74,8 +74,8 @@ class HttpCache extends ActionFilter
$etag
=
$this
->
generateEtag
(
$seed
);
}
$
this
->
sendCacheControlHeader
();
$
response
=
Yii
::
$app
->
getResponse
(
);
$
response
=
$action
->
getResponse
();
$
this
->
sendCacheControlHeader
(
$response
);
if
(
$etag
!==
null
)
{
$response
->
getHeaders
()
->
set
(
'Etag'
,
$etag
);
}
...
...
@@ -109,12 +109,13 @@ class HttpCache extends ActionFilter
/**
* Sends the cache control header to the client
* @param Response $response
* @see cacheControl
*/
protected
function
sendCacheControlHeader
()
protected
function
sendCacheControlHeader
(
$response
)
{
session_cache_limiter
(
'public'
);
$headers
=
Yii
::
$app
->
getResponse
()
->
getHeaders
();
$headers
=
$response
->
getHeaders
();
$headers
->
set
(
'Pragma'
);
if
(
$this
->
cacheControlHeader
!==
null
)
{
$headers
->
set
(
'Cache-Control'
,
$this
->
cacheControlHeader
);
...
...
framework/yii/web/Request.php
View file @
9d7597d6
...
...
@@ -788,7 +788,7 @@ class Request extends \yii\base\Request
$this
->
_csrfCookie
=
$this
->
getCookies
()
->
get
(
$this
->
csrfTokenName
);
if
(
$this
->
_csrfCookie
===
null
)
{
$this
->
_csrfCookie
=
$this
->
createCsrfCookie
();
Yii
::
$app
->
getResponse
()
->
getCookies
()
->
add
(
$this
->
_csrfCookie
);
Yii
::
$app
->
controller
->
getResponse
()
->
getCookies
()
->
add
(
$this
->
_csrfCookie
);
}
}
...
...
framework/yii/web/User.php
View file @
9d7597d6
...
...
@@ -283,8 +283,7 @@ class User extends Component
$this
->
setReturnUrl
(
$request
->
getUrl
());
}
if
(
$this
->
loginUrl
!==
null
)
{
$response
=
Yii
::
$app
->
getResponse
();
$response
->
redirect
(
$this
->
loginUrl
)
->
send
();
Yii
::
$app
->
controller
->
getResponse
()
->
redirect
(
$this
->
loginUrl
)
->
send
();
exit
();
}
else
{
throw
new
HttpException
(
403
,
Yii
::
t
(
'yii'
,
'Login Required'
));
...
...
@@ -372,7 +371,7 @@ class User extends Component
$cookie
=
new
Cookie
(
$this
->
identityCookie
);
$cookie
->
value
=
$value
;
$cookie
->
expire
=
time
()
+
(
int
)
$data
[
2
];
Yii
::
$app
->
getResponse
()
->
getCookies
()
->
add
(
$cookie
);
Yii
::
$app
->
controller
->
getResponse
()
->
getCookies
()
->
add
(
$cookie
);
}
}
}
...
...
@@ -395,7 +394,7 @@ class User extends Component
$duration
,
));
$cookie
->
expire
=
time
()
+
$duration
;
Yii
::
$app
->
getResponse
()
->
getCookies
()
->
add
(
$cookie
);
Yii
::
$app
->
controller
->
getResponse
()
->
getCookies
()
->
add
(
$cookie
);
}
/**
...
...
@@ -429,7 +428,7 @@ class User extends Component
$this
->
sendIdentityCookie
(
$identity
,
$duration
);
}
}
elseif
(
$this
->
enableAutoLogin
)
{
Yii
::
$app
->
getResponse
()
->
getCookies
()
->
remove
(
new
Cookie
(
$this
->
identityCookie
));
Yii
::
$app
->
controller
->
getResponse
()
->
getCookies
()
->
remove
(
new
Cookie
(
$this
->
identityCookie
));
}
}
...
...
framework/yii/web/VerbFilter.php
View file @
9d7597d6
...
...
@@ -80,7 +80,7 @@ class VerbFilter extends Behavior
if
(
!
in_array
(
$verb
,
$allowed
))
{
$event
->
isValid
=
false
;
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.7
Yii
::
$app
->
getResponse
()
->
getHeaders
()
->
set
(
'Allow'
,
implode
(
', '
,
$allowed
));
$event
->
action
->
getResponse
()
->
getHeaders
()
->
set
(
'Allow'
,
implode
(
', '
,
$allowed
));
throw
new
HttpException
(
405
,
'Method Not Allowed. This url can only handle the following request methods: '
.
implode
(
', '
,
$allowed
));
}
}
...
...
tests/unit/framework/helpers/HtmlTest.php
View file @
9d7597d6
...
...
@@ -17,9 +17,6 @@ class HtmlTest extends TestCase
'class'
=>
'yii\web\Request'
,
'url'
=>
'/test'
,
),
'response'
=>
array
(
'class'
=>
'yii\web\Response'
,
),
),
));
}
...
...
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