Commit 364153cd by Carsten Brandt

edited "hello" guide

parent 8b96f51f
......@@ -2,27 +2,27 @@ Saying Hello
============
This section describes how to create a new "Hello" page in your application.
To achieve this goal, you will create an [action](structure-controllers.md) and
To achieve this goal, you will create an [action](structure-controllers.md#creating-actions) and
a [view](structure-views.md):
* 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
* and the action will in turn render the view that shows the word "Hello" to the end user.
Through this tutorial, you will learn how:
Through this tutorial, you will learn three things:
* To create an [action](structure-controllers.md) to respond to requests
* To create a [view](structure-views.md) to compose the response's content
* An application dispatches requests to [actions](structure-controllers.md)
1. How to create an [action](structure-controllers.md) to respond to requests,
2. how to create a [view](structure-views.md) to compose the response's content, and
3. how an application dispatches requests to [actions](structure-controllers.md#creating-actions).
Creating an Action <a name="creating-action"></a>
------------------
For the "Hello" task, you will create a `say` [action](structure-controllers.md) that reads
For the "Hello" task, you will create a `say` [action](structure-controllers.md#creating-actions) that reads
a `message` parameter from the request and displays that message back to the user. If the request
does not provide a `message` parameter, the action will display the default "Hello" message.
> Info: [Actions](structure-controllers.md) are the objects that end users can directly refer to for
> Info: [Actions](structure-controllers.md#creating-actions) 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.
......@@ -48,17 +48,21 @@ 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 the `SiteController` 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 maps to the action's ID.
The name after the `action` prefix maps to the action's ID.
When it comes to naming your actions, you should understand how Yii treats action IDs. Action IDs are always referenced in lower case. If an action ID requires multiple words, they will be concatenated by dashes (e.g., `create-comment`). Action method names are mapped to action IDs by removing any dashes from the IDs,
capitalizing the first letter in each word, and prefixing the resulting with `action`. For example,
the action ID `create-comment` corresponds to the action method name `actionCreateComment`.
When it comes to naming your actions, you should understand how Yii treats action IDs. Action IDs are always
referenced in lower case. If an action ID requires multiple words, they will be concatenated by dashes
(e.g., `create-comment`). Action method names are mapped to action IDs by removing any dashes from the IDs,
capitalizing the first letter in each word, and prefixing the resulting with `action`. For example,
the action ID `create-comment` corresponds to the action method name `actionCreateComment`.
The action method takes a parameter `$message`, whose value defaults to `"Hello"` (in exatly the same way you set a default value for any function or method argument in PHP). When the application
The action method in our example takes a parameter `$message`, whose value defaults to `"Hello"` (in exactly
the same way you set a default value for any function or method argument in PHP). When the application
receives a request and determines that the `say` action is responsible for handling said request, the application will
populate this parameter with the same named parameter found in the request. In other words, if the request includes a `message` paremter with a value of `"Goodbye"`, the $message variable within the action will be assigned that value.
populate this parameter with the same named parameter found in the request. In other words, if the request includes
a `message` parameter with a value of `"Goodbye"`, the `$message` variable within the action will be assigned that value.
Within the action method, [[yii\web\Controller::render()|render()]] is called to render
a [view](structure-views.md) file named `say`. The `message` parameter is also passed to the view
......@@ -80,7 +84,7 @@ use yii\helpers\Html;
```
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`.
is called in an action, it will look for a PHP file named as `views/ControllerID/ViewName.php`.
Note that in the above code, the `message` parameter is [[yii\helpers\Html::encode()|HTML-encoded]]
before being printed. This is necessary as the parameter comes from an end user, making it vulnerable to
......@@ -95,7 +99,7 @@ The content printed by the view script will be returned to the application as th
Trying it Out <a name="trying-it-out"></a>
-------------
After creating the action and the view, you may access the new page by the following URL:
After creating the action and the view, you may access the new page by accessing the following URL:
```
http://hostname/index.php?r=site/say&message=Hello+World
......@@ -109,9 +113,10 @@ If you omit the `message` parameter in the URL, you would see the page display j
the default value of `"Hello"` will be used instead.
> 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`.
method will automatically embed the result of the `say` view in a so-called [layout](structure-views.md#layouts) which in this
case is located at `views/layouts/main.php`.
The `r` parameter requires more explanation. It stands for [route](runtime-routing.md), a globally unique ID
The `r` parameter in the above URL requires more explanation. It stands for [route](runtime-routing.md), an application wide unique ID
that refers to an action. The route's format is `ControllerID/ActionID`. When the application receives
a request, it will check this parameter, using the `ControllerID` part to determine which controller
class should be instantiated to handle the request. Then, the controller will use the `ActionID` part
......
Running Applications
====================
After installing Yii, you have a working Yii application that can be accessed via the URL `http://hostname/basic/web/index.php` or `http://hostname/index.php`, depending upon your configuration. This section will introduce the application's built-in fucntionality, how the code is organized,
and how the application handles requests in general.
After installing Yii, you have a working Yii application that can be accessed via
the URL `http://hostname/basic/web/index.php` or `http://hostname/index.php`, depending
upon your configuration. This section will introduce the application's built-in functionality,
how the code is organized, and how the application handles requests in general.
> Info: For simplicity, throughout this "Getting Started" tutorial, it's assumed that you have set `basic/web`
as the document root of your Web server, and configured, the URL for accessing
......@@ -11,14 +13,14 @@ and how the application handles requests in general.
Functionality <a name="functionality"></a>
---------------
-------------
The basic application installed contains four pages:
* the homepage, displayed when you access the URL `http://hostname/index.php`
* the "About" page
* the "Contact" page displays a contact form that allows end users to contact you via email
* the "Login" page displays a login form that can be used to authenticate end users. Try logging in
* The homepage, displayed when you access the URL `http://hostname/index.php`,
* the "About" page,
* the "Contact" page, which displays a contact form that allows end users to contact you via email,
* and the "Login" page, which displays a login form that can be used to authenticate end users. Try logging in
with "admin/admin", and you will find the "Login" main menu item will change to "Logout".
These pages share a common header and footer. The header contains a main menu bar to allow navigation
......@@ -46,13 +48,13 @@ basic/ application base path
vendor/ contains the installed Composer packages, including the Yii framework itself
views/ contains view files
web/ application Web root, contains Web accessible files
assets/ contains published asset files (js, css) by Yii
assets/ contains published asset files (javascript and css) by Yii
index.php the entry (or bootstrap) script for the application
yii the Yii console command execution script
```
In general, the files in the application can be divided into two types: those under `basic/web` and those
under other directories. The former can be directly accessed from via HTTP (i.e., in a browser), while the latter can not and should not be.
under other directories. The former can be directly accessed via HTTP (i.e., in a browser), while the latter can not and should not be.
Yii implements the [model-view-controller (MVC)](http://wikipedia.org/wiki/Model-view-controller) design pattern,
which is reflected in the above directory organization. The `models` directory contains all [model classes](structure-models.md),
......
......@@ -188,7 +188,7 @@ You may change the default controller with the following [application configurat
## Creating Actions <a name="creating-actions"></a>
Creating actions can be as simple as defining the so-called *action methods*. An action method is
Creating actions can be as simple as defining the so-called *action methods* in a controller class. An action method is
a *public* method whose name starts with the word `action`. The return value of an action method represents
the response data to be sent to end users. The following code defines two actions `index` and `hello-world`:
......
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