Commit 3113e684 by Qiang Xue

minor edits [skip ci]

parent d3108278
Saying Hello Saying Hello
============ ============
In this section, we will describe how to add to your application a new page that displays "Hello". In this section, we will describe how to create a new page displaying "Hello" in your application.
To achieve this goal, you will create an [action](structure-controllers.md) as well as To achieve this goal, you will create an [action](structure-controllers.md) as well as
a [view](structure-views.md). Yii dispatches the page request to the action which in turn a [view](structure-views.md):
renders the view with "Hello" to the user.
* The application will dispatch the page request to the action;
* And the action will in turn render the view that shows "Hello" to the end user.
Through this tutorial, you will learn Through this tutorial, you will learn
...@@ -16,16 +18,23 @@ Through this tutorial, you will learn ...@@ -16,16 +18,23 @@ Through this tutorial, you will learn
Creating an Action Creating an Action
------------------ ------------------
[Actions](structure-controllers.md) are the only objects that end users can directly refer to and request For the "Hello" task, you will create a `say` [action](structure-controllers.md) which reads
for execution. Actions are grouped by [controllers](structure-controllers.md). The execution result of a `message` parameter from a request and displays the message back to the user. If the request
an action is the response that an end user will receive. does not provide a `message` parameter, the action will display the default "Hello".
> Info: [Actions](structure-controllers.md) are the objects that end users can directly refer to for
execution. Actions are grouped by [controllers](structure-controllers.md). The execution result of
an action is the response that an end user will receive.
For the "Hello" task, you will create a `say` action which reads a `message` parameter from Actions must be declared in [controllers](structure-controllers.md). For simplicity, you may
a request and displays the message content back to the user. If the `message` parameter is not given, declare the `say` action in the existing controller `SiteController` which is defined
it will use the default value "Hello". For simplicity, you may put this action in an existing in the class file `controllers/SiteController.php`:
controller `SiteController` which is defined in the class file `controllers/SiteController.php`:
```php ```php
namespace app\controllers;
use yii\web\Controller;
class SiteController extends Controller class SiteController extends Controller
{ {
// ...existing code... // ...existing code...
...@@ -38,27 +47,29 @@ class SiteController extends Controller ...@@ -38,27 +47,29 @@ class SiteController extends Controller
``` ```
In the above code, the `say` action is defined as a method named `actionSay` in `SiteController`. In the above code, the `say` action is defined as a method named `actionSay` in `SiteController`.
Yii uses the prefix `action` to differentiate action methods from non-action methods in the class. Yii uses the prefix `action` to differentiate action methods from non-action methods in a controller class.
The name after the `action` prefix is treated as the ID of the corresponding action. The name after the `action` prefix is treated as the ID of the corresponding action.
> Info: Actions defined by action methods are called *inline actions*. Yii will create an [[yii\base\InlineAction|InlineAction]] > Info: Action IDs are in lower case. If an action ID has multiple words, they should be concatenated by dashes,
object during runtime which will call the corresponding action method to handle a request. e.g., `create-comment`. Action method names are derived from action IDs by removing dashes from the IDs,
turning the first letter in each word into upper case, and prefixing them with `action`. For example,
the action ID `create-comment` corresponds to the action method name `actionCreateComment`.
The action method takes a parameter `$message` which defaults to `"Hello"`. When the application The action method takes a parameter `$message` which defaults to `"Hello"`. When the application
receives a request and determines the `say` action is responsible to handle the request, it will receives a request and determines that the `say` action is responsible for handling the request, it will
populate this parameter with the same named parameter found in the request. populate this parameter with the same named parameter found in the request.
Within the action method, the [[yii\web\Controller::render()|render()]] method is called which Within the action method, [[yii\web\Controller::render()|render()]] is called to render
renders a [view](structure-views.md) named `say` and passes along the `message` parameter. The rendering a [view](structure-views.md) named `say`. The `message` parameter is also passed to the view
result is returned by the action method, which will be taken by the application and displayed to the end user. so that it can be echoed there. The rendering result is returned by the action method, which will be taken
by the application and displayed to the end user.
Creating a View Creating a View
--------------- ---------------
[Views](structure-views.md) are scripts that you write to compose response content. [Views](structure-views.md) are scripts that you write to compose response content.
For the "Hello" task, you will create a `say` view which echoes the `message` parameter For the "Hello" task, you will create a `say` view to echo the `message` parameter received from the action method:
passed from the `say` action when it calls `render()`:
```php ```php
<?php <?php
...@@ -67,17 +78,17 @@ use yii\helpers\Html; ...@@ -67,17 +78,17 @@ use yii\helpers\Html;
<?= Html::encode($message) ?> <?= Html::encode($message) ?>
``` ```
The `say` view should be saved in the file `views/site/say.php`. When the method [[yii\web\Controller::render()|render()]]
is called in an action, it will look for a PHP file named as `views/ControllerID/ActionID/ViewName.php`.
Note that in the above code, the `message` parameter is [[yii\helpers\Html::encode()|HTML-encoded]] Note that in the above code, the `message` parameter is [[yii\helpers\Html::encode()|HTML-encoded]]
before being echoed. This is necessary because the parameter is coming from end users who may attempt before being echoed. This is necessary because the parameter comes from end users who may attempt
[cross-site scripting (XSS) attacks](http://en.wikipedia.org/wiki/Cross-site_scripting) by embedding [cross-site scripting (XSS) attacks](http://en.wikipedia.org/wiki/Cross-site_scripting) by embedding
malicious JavaScript code in the parameter. malicious JavaScript code in the parameter.
The `say` view should be saved in the file `views/site/say.php`. When the method [[yii\web\Controller::render()|render()]] You may put more content in the `say` view. They can be HTML tags, plain text, or even PHP statements.
is called in an action, it will look for a PHP file named as `views/ControllerID/ActionID/ViewName.php`. In fact, the `say` view is just a PHP script which is executed by the [[yii\web\Controller::render()|render()]] method.
The content echoed by the view script will be forwarded by the application as the response to the end user.
> Tip: You may put more content in the `say` view. They can be HTML tags, plain text, or even PHP statements.
In fact, the `say` view is just a PHP script which is executed by the [[yii\web\Controller::render()|render()]] method.
The content echoed by the view script will be forwarded by the application as the response to the end user.
How It Works How It Works
...@@ -94,6 +105,9 @@ the application. If you omit the `message` parameter in the URL, you would see t ...@@ -94,6 +105,9 @@ the application. If you omit the `message` parameter in the URL, you would see t
This is because `message` is passed as a parameter to the `actionSay()` method, and when it is omitted, This is because `message` is passed as a parameter to the `actionSay()` method, and when it is omitted,
the default value of `"Hello"` will come into play. the default value of `"Hello"` will come into play.
> Info: The new page shares the same header and footer as other pages because the [[yii\web\Controller::render()|render()]]
method will automatically embed the result of the `say` view in a so-called [layout](structure-views.md) `views/layouts/main.php`.
The `r` parameter requires more explanation. It stands for [route](runtime-routing.md) which is a globally unique ID The `r` parameter requires more explanation. It stands for [route](runtime-routing.md) which is a globally unique ID
referring to an action. Its format is `ControllerID/ActionID`. When the application receives referring to an action. Its format is `ControllerID/ActionID`. When the application receives
a request, it will check this parameter and use the `ControllerID` part to determine which controller a request, it will check this parameter and use the `ControllerID` part to determine which controller
...@@ -102,13 +116,20 @@ to determine which action should be instantiated to do the real work. In our cas ...@@ -102,13 +116,20 @@ to determine which action should be instantiated to do the real work. In our cas
will be resolved into the `SiteController` controller class and the `say` action. As a result, will be resolved into the `SiteController` controller class and the `say` action. As a result,
the `SiteController::actionSay()` method will be called to handle the request. the `SiteController::actionSay()` method will be called to handle the request.
> Info: Like actions, controllers also have IDs that uniquely identify them in an application.
Controller IDs use the same naming rules as action IDs. Controller class names are derived from
controller IDs by removing dashes from the IDs, turning the first letter in each word into upper case,
and suffixing them with the word `Controller`. For example, the controller ID `post-comment` corresponds
to the controller class name `PostCommentController`.
Summary Summary
------- -------
In this section, you have touched the controller part and the view part in the MVC design pattern. In this section, you have touched the controller part and the view part in the MVC design pattern.
You created an action as part of a controller to handle requests. And you also created a view script You created an action as part of a controller to handle requests. And you also created a view
to compose response content. There is no model involved in this task because the only data used is to compose response content. There is no model involved because the only data used is the simple `message` parameter.
the simple `message` parameter.
You also have learned the route concept which is the bridge between user requests and controller actions.
In the next section, you will get into touch with the model part through building HTML forms. In the next section, you will learn how to create a model and add a new page with an HTML form.
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