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
bc19fd50
Commit
bc19fd50
authored
Jun 01, 2014
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
guide WIP [skip ci]
parent
01e11e37
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
63 additions
and
4 deletions
+63
-4
README.md
docs/guide/README.md
+3
-4
output-theming.md
docs/guide/output-theming.md
+0
-0
runtime-handling-errors.md
docs/guide/runtime-handling-errors.md
+0
-0
runtime-logging.md
docs/guide/runtime-logging.md
+0
-0
structure-controllers.md
docs/guide/structure-controllers.md
+60
-0
No files found.
docs/guide/README.md
View file @
bc19fd50
...
...
@@ -52,7 +52,8 @@ Handling Requests
*
**TBD**
[
Responses
](
runtime-responses.md
)
*
**TBD**
[
Sessions and Cookies
](
runtime-sessions-cookies.md
)
*
[
URL Parsing and Generation
](
runtime-url-handling.md
)
*
[
Handling Errors
](
runtime-handling-errors.md
)
*
[
Logging
](
runtime-logging.md
)
Key Concepts
...
...
@@ -100,6 +101,7 @@ Displaying Data
*
[
Data Providers
](
output-data-providers.md
)
*
[
Data Widgets
](
output-data-widgets.md
)
*
[
Managing Assets
](
output-assets.md
)
*
[
Theming
](
output-theming.md
)
Security
...
...
@@ -171,14 +173,11 @@ Special Topics
*
[
Advanced Application Template
](
tutorial-advanced-app.md
)
*
[
Building Application from Scratch
](
tutorial-start-from-scratch.md
)
*
[
Console Commands
](
tutorial-console.md
)
*
[
Handling Errors
](
tutorial-handling-errors.md
)
*
[
Internationalization
](
tutorial-i18n.md
)
*
[
Logging
](
tutorial-logging.md
)
*
[
Mailing
](
tutorial-mailing.md
)
*
[
Performance Tuning
](
tutorial-performance-tuning.md
)
*
**TBD**
[
Shared Hosting Environment
](
tutorial-shared-hosting.md
)
*
[
Template Engines
](
tutorial-template-engines.md
)
*
[
Theming
](
tutorial-theming.md
)
Widgets
...
...
docs/guide/
tutorial
-theming.md
→
docs/guide/
output
-theming.md
View file @
bc19fd50
File moved
docs/guide/
tutorial
-handling-errors.md
→
docs/guide/
runtime
-handling-errors.md
View file @
bc19fd50
File moved
docs/guide/
tutorial
-logging.md
→
docs/guide/
runtime
-logging.md
View file @
bc19fd50
File moved
docs/guide/structure-controllers.md
View file @
bc19fd50
...
...
@@ -382,3 +382,63 @@ to fulfill the request:
*
By default, each
`afterAction()`
method call will trigger an
`afterAction`
event to which you can attach a handler.
6.
The application will take the action result and assign it to the
[
response
](
runtime-responses.md
)
.
## Best Practices <a name="best-practices"></a>
In a well-designed application, controllers are often very thin with each action containing only a few lines of code.
The main role of these code is to invoke appropriate
[
models
](
structure-models.md
)
with the request data
and use
[
views
](
structure-views.md
)
to present the models.
The following code is a typical example showing how the
`view`
and
`create`
actions should be implemented
in a controller.
```
php
namespace
app\controllers
;
use
Yii
;
use
app\models\Post
;
use
yii\web\Controller
;
use
yii\web\NotFoundHttpException
;
class
PostController
extends
Controller
{
public
function
actionView
(
$id
)
{
$model
=
Post
::
findOne
(
$id
);
if
(
$model
!==
null
)
{
return
$this
->
render
(
'view'
,
[
'model'
=>
$models
,
]);
}
else
{
throw
new
NotFoundHttpException
;
}
}
public
function
actionCreate
()
{
$model
=
new
Post
;
if
(
$model
->
load
(
Yii
::
$app
->
request
->
post
())
&&
$model
->
save
())
{
return
$this
->
redirect
([
'view'
,
'id'
=>
$model
->
id
]);
}
else
{
return
$this
->
render
(
'create'
,
[
'model'
=>
$model
,
]);
}
}
}
```
In the
`view`
action, the code first loads the model according to the requested model ID; If the model
is loaded successfully, it will display it using the view named
`view`
. Otherwise, it will throw an exception.
In the
`create`
action, the code is similar. It first tries to populate the model using the request data
and save the model. If both succeed it will redirect the browser to the
`view`
action with the ID of
the newly model. Otherwise it will display the
`create`
view through which users can provide the needed input.
In summary, the code in a controller may access the
[
request
](
runtime-requests.md
)
and
[
response
](
runtime-responses.md
)
objects,
[
models
](
structure-models.md
)
and
[
views
](
structure-views.md
)
. It should not, however, try to
process the request data or build up response result - those are the jobs of
[
models
](
structure-models.md
)
and
[
views
](
structure-views.md
)
. If your controller is rather complicated, it is usually an indication that
you should refactor your controller code.
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