In this section, we describe how to create a skeleton application that will serve as our starting point. For simplicity, we assume that the document root of our Web server is /wwwroot
and the corresponding URL is http://www.example.com/
.
We first install the Yii framework. Grab a copy of the Yii release file (version 1.1.1 or above) from www.yiiframework.com and unpack it to the directory /wwwroot/yii
. Double check to make sure that there is a directory /wwwroot/yii/framework
.
Tip: The Yii framework can be installed anywhere in the file system, not necessarily under a Web folder. Its
framework
directory contains all framework code and is the only framework directory needed when deploying an Yii application. A single installation of Yii can be used by multiple Yii applications.
After installing Yii, open a browser window and access the URL http://www.example.com/yii/requirements/index.php
. It shows the requirement checker provided in the Yii release. For our blog application, besides the minimal requirements needed by Yii, we also need to enable both the pdo
and pdo_sqlite
PHP extensions so that we can access SQLite databases.
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.
We have an entry script file /wwwroot/blog/index.php
which has the following content:
$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.
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.
To help understand how Yii works, we describe the main workflow in our skeleton application when a user is accessing its contact page:
http://www.example.com/blog/index.php?r=site/contact
;/wwwroot/blog/protected/config/main.php
;site
controller and the contact
action (the actionContact
method in /wwwroot/blog/protected/controllers/SiteController.php
);site
controller in terms of a SiteController
instance and then executes it;SiteController
instance executes the contact
action by calling its actionContact()
method;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
.
Found a typo or you think this page needs improvement?
Edit it on github !
Using yiic with MAMP
You might need to add the MAMP version of PHP to your PATH
For example:
export PATH=/Applications/MAMP/bin/php5/bin:${PATH}
Additional tip on yiic tool when using xampp
If your php executable is not added to your path, then use similar command in your windows command prompt:
{path to php.exe} {path to yiic.php} {path to new web app}
For example:
E:\xampp\php\php.exe E:\xampp\htdocs\yiicore\framewor\yiic.php webapp E:\xampp\htdocs\yiiapp
Problems you might encounter
Need a way to SSH into your Web server? Use PuTTY. Just Google it.
To easily find your path/to/php enter the following once you've logged in to your server using PuTTY:
which php
It will return something like this: /usr/local/bin/php In fact, you might as well try this if needed because more than likely yours will be the same.
I'm not positive on this but you might need to create the /blog folder ahead of time. I ran into problems and this was one thing in a series of steps that fixed the problems for me, but I didn't confirm that this was required. It might be that the yiic script will do it if it's missing.
If you try to execute the unix command above to build the skeleton application and get a "Permission Denied" error, more than likely you need to change the permissions on the /yii/framework/yiic file to make it executable. If you chmod the file to 754 you should be fine.
PHP not on your PATH, when using XAMPP in Windows
Comment #1312 by HossainK is now outdated.
Newest versions of XAMPP uses reinvented
setup_xampp.bat
, which uses PHP commands for many operations. If you don't have a valid path to PHP.exe among your PATH variable, you won't be able to run it, and therefore -- to install XAMPP itself.Haveing valid path to PHP.exe in PATH variable is no obligatory.
Signup or Login in order to comment.