Commit 595faca9 by Qiang Xue

Finished the quick start guide. [skip ci]

parent b80c0c3b
...@@ -89,6 +89,8 @@ To represent and fetch the data in the `country` table, create an [Active Record ...@@ -89,6 +89,8 @@ To represent and fetch the data in the `country` table, create an [Active Record
class named `Country` and save it in the file `models/Country.php`. class named `Country` and save it in the file `models/Country.php`.
```php ```php
<?php
namespace app\models; namespace app\models;
use yii\db\ActiveRecord; use yii\db\ActiveRecord;
...@@ -136,6 +138,8 @@ for all actions about manipulating country data. Name this new controller as `Co ...@@ -136,6 +138,8 @@ for all actions about manipulating country data. Name this new controller as `Co
an `index` action in it, as shown in the following, an `index` action in it, as shown in the following,
```php ```php
<?php
namespace app\controllers; namespace app\controllers;
use yii\web\Controller; use yii\web\Controller;
...@@ -193,6 +197,7 @@ with the following content: ...@@ -193,6 +197,7 @@ with the following content:
use yii\helpers\Html; use yii\helpers\Html;
use yii\widgets\LinkPager; use yii\widgets\LinkPager;
?> ?>
<h1>Countries</h1>
<ul> <ul>
<?php foreach ($countries as $country): ?> <?php foreach ($countries as $country): ?>
<li> <li>
...@@ -220,6 +225,8 @@ To see how it works, use your browser to access the following URL: ...@@ -220,6 +225,8 @@ To see how it works, use your browser to access the following URL:
http://hostname/index.php?r=country/index http://hostname/index.php?r=country/index
``` ```
![Country List](images/start-country-list.png)
You will see a page showing five countries. And below the countries, you will see a pager with four buttons. You will see a page showing five countries. And below the countries, you will see a pager with four buttons.
If you click on the button "2", you will see that the page displays another five countries in the database. If you click on the button "2", you will see that the page displays another five countries in the database.
Observe more carefully and you will find the URL in the browser changes to Observe more carefully and you will find the URL in the browser changes to
......
...@@ -23,6 +23,8 @@ save the class in the file `models/EntryForm.php`. Please refer to the [Class Au ...@@ -23,6 +23,8 @@ save the class in the file `models/EntryForm.php`. Please refer to the [Class Au
section for more details about the class file naming convention. section for more details about the class file naming convention.
```php ```php
<?php
namespace app\models; namespace app\models;
use yii\base\Model; use yii\base\Model;
...@@ -64,6 +66,8 @@ Creating an Action <a name="creating-action"></a> ...@@ -64,6 +66,8 @@ Creating an Action <a name="creating-action"></a>
Next, create an `entry` action in the `site` controller, like you did in the previous section. Next, create an `entry` action in the `site` controller, like you did in the previous section.
```php ```php
<?php
namespace app\controllers; namespace app\controllers;
use Yii; use Yii;
...@@ -170,9 +174,14 @@ is also displayed indicating what data you need to enter. If you click the submi ...@@ -170,9 +174,14 @@ is also displayed indicating what data you need to enter. If you click the submi
entering anything, or if you do not provide a valid email address, you will see an error message that entering anything, or if you do not provide a valid email address, you will see an error message that
is displayed next to each problematic input field. is displayed next to each problematic input field.
![Form with Validation Errors](images/start-form-validation.png)
After entering a valid name and email address and clicking the submit button, you will see a new page After entering a valid name and email address and clicking the submit button, you will see a new page
displaying the data that you just entered. displaying the data that you just entered.
![Confirmation of Data Entry](images/start-entry-confirmation.png)
### Magic Explained <a name="magic-explained"></a> ### Magic Explained <a name="magic-explained"></a>
......
...@@ -45,26 +45,28 @@ Therefore, your application has already enabled Gii, and you can access it via t ...@@ -45,26 +45,28 @@ Therefore, your application has already enabled Gii, and you can access it via t
http://hostname/index.php?r=gii http://hostname/index.php?r=gii
``` ```
![Gii](images/start-gii.png)
Generating an Active Record Class <a name="generating-ar"></a> Generating an Active Record Class <a name="generating-ar"></a>
--------------------------------- ---------------------------------
To use Gii to generate an Active Record class, select the "Model Generator". You will see the following page: To use Gii to generate an Active Record class, select the "Model Generator" and fill out the form as follows,
Fill out the form as follows:
* Table Name: `country` * Table Name: `country`
* Model Class: `Country` * Model Class: `Country`
![Model Generator](images/start-gii-model.png)
Click on the "Preview" button. You will see `models/Country.php` is listed in the result. Click on the "Preview" button. You will see `models/Country.php` is listed in the result.
You may click on it to preview its content. You may click on it to preview its content.
Because in the last section, you have already created the same file `models/Country.php`, if you click Because in the last section, you have already created the same file `models/Country.php`, if you click
the `diff` button next to the file name, you will see the difference between the code to be generated the `diff` button next to the file name, you will see the difference between the code to be generated
and the code that you have already written. and the code that you have already written.
![Model Generator Preview](images/start-gii-model-preview.png)
Check the checkbox next to "overwrite" and then click on the "Generate" button. You will see Check the checkbox next to "overwrite" and then click on the "Generate" button. You will see
a confirmation page indicating the code has been successfully generated and your existing `models/Country.php` a confirmation page indicating the code has been successfully generated and your existing `models/Country.php`
is overwritten with the newly generated code. is overwritten with the newly generated code.
...@@ -79,12 +81,13 @@ To create CRUD code, select the "CRUD Generator". Fill out the form as follows: ...@@ -79,12 +81,13 @@ To create CRUD code, select the "CRUD Generator". Fill out the form as follows:
* Search Model Class: `app\models\CountrySearch` * Search Model Class: `app\models\CountrySearch`
* Controller Class: `app\controllers\CountryController` * Controller Class: `app\controllers\CountryController`
Click on the "Preview" button. You will see a list of files to be generated, as shown below. ![CRUD Generator](images/start-gii-crud.png)
Click on the "Preview" button. You will see a list of files to be generated, as shown below.
Make sure you have checked the checkbox for the `controllers/CityController.php` line. This is needed Make sure you have checked the overwrite checkboxes for both `controllers/CountryController.php` and
because you have already created this file in the previous section and the file needs to be overwritten `views/country/index.php` files. This is needed because you have already created these files
to have full CRUD support. in the previous section and you want to have them overwritten to have full CRUD support.
How It Works <a name="how-it-works"></a> How It Works <a name="how-it-works"></a>
...@@ -102,6 +105,10 @@ or filter it by entering filter conditions in the column headers. ...@@ -102,6 +105,10 @@ or filter it by entering filter conditions in the column headers.
For each country displayed in the grid, you may choose to view its detail, update it or delete it. For each country displayed in the grid, you may choose to view its detail, update it or delete it.
You may also click on the "Create Country" button on top of the grid to create a new country. You may also click on the "Create Country" button on top of the grid to create a new country.
![Data Grid of Countries](images/start-gii-country-grid.png)
![Updating a Country](images/start-gii-country-update.png)
The following is the list of the generated files in case you want to dig out how these features are implemented, The following is the list of the generated files in case you want to dig out how these features are implemented,
or if you want to customize them. or if you want to customize them.
......
...@@ -31,6 +31,8 @@ declare the `say` action in the existing controller `SiteController` which is de ...@@ -31,6 +31,8 @@ declare the `say` action in the existing controller `SiteController` which is de
in the class file `controllers/SiteController.php`: in the class file `controllers/SiteController.php`:
```php ```php
<?php
namespace app\controllers; namespace app\controllers;
use yii\web\Controller; use yii\web\Controller;
...@@ -100,6 +102,8 @@ After creating the action and the view, you may access the new page by the follo ...@@ -100,6 +102,8 @@ After creating the action and the view, you may access the new page by the follo
http://hostname/index.php?r=site/say&message=Hello+World http://hostname/index.php?r=site/say&message=Hello+World
``` ```
![Hello World](images/start-hello-world.png)
This will show a page displaying "Hello World". The page shares the same header and footer as other pages of This will show a page displaying "Hello World". The page shares the same header and footer as other pages of
the application. If you omit the `message` parameter in the URL, you would see the page displays "Hello". the application. If you omit the `message` parameter in the URL, you would see the page displays "Hello".
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,
......
...@@ -73,7 +73,9 @@ and the server name is `hostname`, ...@@ -73,7 +73,9 @@ and the server name is `hostname`,
http://hostname/basic/web/index.php http://hostname/basic/web/index.php
``` ```
You should see a "Congratulations!" page in your browser. If not, please check if your PHP installation satisfies ![Successful Installation of Yii](images/start-app-installed.png)
You should see the above "Congratulations!" page in your browser. If not, please check if your PHP installation satisfies
Yii's requirements by using one of the following approaches: Yii's requirements by using one of the following approaches:
* Use a browser to access the URL `http://hostname/basic/requirements.php` * Use a browser to access the URL `http://hostname/basic/requirements.php`
......
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