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
bc241eaf
Commit
bc241eaf
authored
Nov 20, 2014
by
Christopher Vrooman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update runtime-responses.md
Minor syntax changes.
parent
5a52503d
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
12 additions
and
12 deletions
+12
-12
runtime-responses.md
docs/guide/runtime-responses.md
+12
-12
No files found.
docs/guide/runtime-responses.md
View file @
bc241eaf
...
@@ -69,7 +69,7 @@ $headers->add('Pragma', 'no-cache');
...
@@ -69,7 +69,7 @@ $headers->add('Pragma', 'no-cache');
// set a Pragma header. Any existing Pragma headers will be discarded.
// set a Pragma header. Any existing Pragma headers will be discarded.
$headers
->
set
(
'Pragma'
,
'no-cache'
);
$headers
->
set
(
'Pragma'
,
'no-cache'
);
// remove Pragma header(s) and return the removed Pragma header values in array
// remove Pragma header(s) and return the removed Pragma header values in a
n a
rray
$values
=
$headers
->
remove
(
'Pragma'
);
$values
=
$headers
->
remove
(
'Pragma'
);
```
```
...
@@ -107,7 +107,7 @@ You can customize these formatters or add new ones by configuring the [[yii\web\
...
@@ -107,7 +107,7 @@ You can customize these formatters or add new ones by configuring the [[yii\web\
*
[
[yii\web\Response::FORMAT_JSONP|JSONP
]
]: implemented by
[
[yii\web\JsonResponseFormatter
]
].
*
[
[yii\web\Response::FORMAT_JSONP|JSONP
]
]: implemented by
[
[yii\web\JsonResponseFormatter
]
].
*
[
[yii\web\Response::FORMAT_RAW|RAW
]
]: use this format if you want to send the response directly without applying any formatting.
*
[
[yii\web\Response::FORMAT_RAW|RAW
]
]: use this format if you want to send the response directly without applying any formatting.
While response body can be set explicitly as shown above, in most cases you may set it implicitly by the return value
While
the
response body can be set explicitly as shown above, in most cases you may set it implicitly by the return value
of
[
action
](
structure-controllers.md
)
methods. A common use case is like the following:
of
[
action
](
structure-controllers.md
)
methods. A common use case is like the following:
```
php
```
php
...
@@ -120,7 +120,7 @@ public function actionIndex()
...
@@ -120,7 +120,7 @@ public function actionIndex()
The
`index`
action above returns the rendering result of the
`index`
view. The return value will be taken
The
`index`
action above returns the rendering result of the
`index`
view. The return value will be taken
by the
`response`
component, formatted and then sent to end users.
by the
`response`
component, formatted and then sent to end users.
Because by default
, the response format is a
s
[
[yii\web\Response::FORMAT_HTML|HTML
]
], you should only return a string
Because by default
the response format i
s
[
[yii\web\Response::FORMAT_HTML|HTML
]
], you should only return a string
in an action method. If you want to use a different response format, you should set it first before returning the data.
in an action method. If you want to use a different response format, you should set it first before returning the data.
For example,
For example,
...
@@ -154,13 +154,13 @@ public function actionInfo()
...
@@ -154,13 +154,13 @@ public function actionInfo()
> Note: If you are creating your own response objects, you will not be able to take advantage of the configurations
> Note: If you are creating your own response objects, you will not be able to take advantage of the configurations
that you set for the
`response`
component in the application configuration. You can, however, use
that you set for the
`response`
component in the application configuration. You can, however, use
[
dependency injection
](
concept-di-container.md
)
to apply common configuration to your new response objects.
[
dependency injection
](
concept-di-container.md
)
to apply
a
common configuration to your new response objects.
## Browser Redirection <a name="browser-redirection"></a>
## Browser Redirection <a name="browser-redirection"></a>
Browser redirection relies on sending a
`Location`
HTTP header. Because this feature is commonly used, Yii provides
Browser redirection relies on sending a
`Location`
HTTP header. Because this feature is commonly used, Yii provides
some special support
s
for it.
some special support for it.
You can redirect the user browser to a URL by calling the
[
[yii\web\Response::redirect()
]
] method. The method
You can redirect the user browser to a URL by calling the
[
[yii\web\Response::redirect()
]
] method. The method
sets the appropriate
`Location`
header with the given URL and returns the response object itself. In an action method,
sets the appropriate
`Location`
header with the given URL and returns the response object itself. In an action method,
...
@@ -177,7 +177,7 @@ In the above code, the action method returns the result of the `redirect()` meth
...
@@ -177,7 +177,7 @@ In the above code, the action method returns the result of the `redirect()` meth
object returned by an action method will be used as the response sending to end users.
object returned by an action method will be used as the response sending to end users.
In places other than an action method, you should call
[
[yii\web\Response::redirect()
]
] directly followed by
In places other than an action method, you should call
[
[yii\web\Response::redirect()
]
] directly followed by
a call to the
[
[yii\web\Response::send()
]
] method to ensure no extra content will be appended to the response.
a c
hained c
all to the
[
[yii\web\Response::send()
]
] method to ensure no extra content will be appended to the response.
```
php
```
php
\Yii
::
$app
->
response
->
redirect
(
'http://example.com/new'
,
301
)
->
send
();
\Yii
::
$app
->
response
->
redirect
(
'http://example.com/new'
,
301
)
->
send
();
...
@@ -188,8 +188,8 @@ a call to the [[yii\web\Response::send()]] method to ensure no extra content wil
...
@@ -188,8 +188,8 @@ a call to the [[yii\web\Response::send()]] method to ensure no extra content wil
code 301 to tell the browser that the resource has been
*permanently*
relocated.
code 301 to tell the browser that the resource has been
*permanently*
relocated.
When the current request is an AJAX request, sending a
`Location`
header will not automatically cause the browser
When the current request is an AJAX request, sending a
`Location`
header will not automatically cause the browser
redirection
. To solve this problem, the
[
[yii\web\Response::redirect()
]
] method sets an
`X-Redirect`
header with
to redirect
. To solve this problem, the
[
[yii\web\Response::redirect()
]
] method sets an
`X-Redirect`
header with
the redirection URL as its value. On the client side you may write JavaScript code to read this header value and
the redirection URL as its value. On the client side
,
you may write JavaScript code to read this header value and
redirect the browser accordingly.
redirect the browser accordingly.
> Info: Yii comes with a `yii.js` JavaScript file which provides a set of commonly used JavaScript utilities,
> Info: Yii comes with a `yii.js` JavaScript file which provides a set of commonly used JavaScript utilities,
...
@@ -200,11 +200,11 @@ redirect the browser accordingly.
...
@@ -200,11 +200,11 @@ redirect the browser accordingly.
## Sending Files <a name="sending-files"></a>
## Sending Files <a name="sending-files"></a>
Like browser redirection, file sending is another feature that relies on specific HTTP headers. Yii provides
Like browser redirection, file sending is another feature that relies on specific HTTP headers. Yii provides
a set of methods to support various file sending needs. They all have built-in support for HTTP range header.
a set of methods to support various file sending needs. They all have built-in support for
the
HTTP range header.
*
[
[yii\web\Response::sendFile()
]
]: sends an existing file to client.
*
[
[yii\web\Response::sendFile()
]
]: sends an existing file to
a
client.
*
[
[yii\web\Response::sendContentAsFile()
]
]: sends a text string as a file to client.
*
[
[yii\web\Response::sendContentAsFile()
]
]: sends a text string as a file to
a
client.
*
[
[yii\web\Response::sendStreamAsFile()
]
]: sends an existing file stream as a file to client.
*
[
[yii\web\Response::sendStreamAsFile()
]
]: sends an existing file stream as a file to
a
client.
These methods have the same method signature with the response object as the return value. If the file
These methods have the same method signature with the response object as the return value. If the file
to be sent is very big, you should consider using
[
[yii\web\Response::sendStreamAsFile()
]
] because it is more
to be sent is very big, you should consider using
[
[yii\web\Response::sendStreamAsFile()
]
] because it is more
...
...
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