Yii FrameworkPada bagian ini, kami jelaskan bagaimana membuat kerangka aplikasi (application skeleton) yang akan berfungsi sebagai titik awal kita. Untuk mempermudah, kita mengasumsikan bahwa dokumen root server Web kita adalah /wwwroot dan URL yang sesuai http://www.example.com/.

1. Installing Yii

Pertama kita install framework Yii terlebih dahulu. Download salinan file rilis Yii (versi 1.1.1 atau di atas) dari www.yiiframework.com dan ekstrak ke direktori /wwwroot/yii. Periksa untuk memastikan bahwa ada direktori /wwwroot/ yi /framework.

Tip: Yii framework dapat diinstal di mana saja di sistem file, tidak harus di bawah folder Web. Direktori framework yang berisi semua kerangka kode dan direktori framework hanya dibutuhkan ketika deploying aplikasi Yii. Sebuah instalasi tunggal Yii bisa dipakai oleh banyak aplikasi Yii.

Setelah melakukan instalasi Yii, buka browser window dan akses URL http://www.example.com/yii/requirements/index.php. Ini menunjukkan pemeriksa persyaratan yang disediakan dalam rilis Yii. Untuk aplikasi blog kita, selain persyaratan minimal yang dibutuhkan oleh Yii, kita juga perlu mengaktifkan baik PDO maupun ekstensi PHP pdo_sqlite sehingga kita dapat mengakses database SQLite.

2. Creating Skeleton Application

We then use the yiic tool to create a skeleton application under the directory /wwwroot/blog. The yiic tool is a command line tool provided in the Yii release. It can be used to generate code to reduce certain repetitive coding tasks.

Open a command window and execute the following command:

% /wwwroot/yii/framework/yiic webapp /wwwroot/blog
Create a Web application under '/wwwroot/blog'? [Yes|No]y
......

Tip: In order to use the yiic tool as shown above, the CLI PHP program must be on the command search path. If not, the following command may be used instead:

path/to/php /wwwroot/yii/framework/yiic.php webapp /wwwroot/blog

To try out the application we just created, open a Web browser and navigate to the URL http://www.example.com/blog/index.php. We should see that our skeleton application already has four fully functional pages: the homepage, the about page, the contact page and the login page.

In the following, we briefly describe what we have in this skeleton application.

Entry Script

We have an entry script file /wwwroot/blog/index.php which has the following content:

<?php
$yii='/wwwroot/framework/yii.php';
$config=dirname(__FILE__).'/protected/config/main.php';

// remove the following line when in production mode
defined('YII_DEBUG') or define('YII_DEBUG',true);

require_once($yii);
Yii::createWebApplication($config)->run();

This is the only script that Web users can directly access. The script first includes the Yii bootstrap file yii.php. It then creates an application instance with the specified configuration and executes the application.

Base Application Directory

We also have an application base directory /wwwroot/blog/protected. The majority of our code and data will be placed under this directory, and it should be protected from being accessed by Web users. For Apache httpd Web server, we place under this directory a .htaccess file with the following content:

deny from all

For other Web servers, please refer to the corresponding manual on how to protect a directory from being accessed by Web users.

3. Application Workflow

To help understand how Yii works, we describe the main workflow in our skeleton application when a user is accessing its contact page:

  1. The user requests the URL http://www.example.com/blog/index.php?r=site/contact;
  2. The entry script is executed by the Web server to process the request;
  3. An application instance is created and configured with initial property values specified in the application configuration file /wwwroot/blog/protected/config/main.php;
  4. The application resolves the request into a controller and a controller action. For the contact page request, it is resolved as the site controller and the contact action (the actionContact method in /wwwroot/blog/protected/controllers/SiteController.php);
  5. The application creates the site controller in terms of a SiteController instance and then executes it;
  6. The SiteController instance executes the contact action by calling its actionContact() method;
  7. The actionContact method renders a view named contact to the Web user. Internally, this is achieved by including the view file /wwwroot/blog/protected/views/site/contact.php and embedding the result into the layout file /wwwroot/blog/protected/views/layouts/column1.php.

Sumber : http://www.yiiframework.com/doc/blog/1.1/en/start.testdrive