Yii is a web application framework, so the first thing you need is a web server. Yii is capable of running on most common HTTP servers, but the platform of choice for project maintainers and testers is Apache on Linux. NGINX is also a good choice, and you can also run Yii on Windows servers with Apache or Microsoft's IIS server.
All instructions in this section assume the Linux (or Mac OS) environment with an Apache server.
As Yii is a PHP framework, the main dependency of Yii is PHP (version 5.1.0 or higher). Yii also makes use of a few key PHP extensions. These include PDO for MySQL or PostgreSQL, the DOM extension, Mcrypt for security, SOAP, and GD for image processing. For data caching, it requires either Memcache, APC, XCache, or EAccellerator, all of which are optional. Yii supports database solutions such as MySQL 4.1 or greater, PostgreSQL 7.3 and above, SQLite 2 and 3, Microsoft SQL Server 2000 or later, and Oracle. Depending on which database is selected for the project, the appropriate PHP extension will be required. Please note that you don't need a database at all to use Yii in your application, however, you'll definitely lose a lot of data persistence solutions built in the framework.
Let's start from the assumption that you already have the following things:
A running and configured web server such as Apache
A folder published as a webroot of a website accessible from your browser
So the only thing you need to set up right now is a real website in this webroot folder. Properly doing this two things is surely out of scope of this book.
The place where you get the Yii framework bundle is the downloads section at the official website (direct URL is http://www.yiiframework.com/download). For application development, you need a stable version packaged in the archive file. You don't need to get the sources of Yii from a GitHub or Subversion repository unless you want to fiddle with the framework itself.
The ideology behind Yii is that the framework itself lies outside of your application directory, preferably in some system directory with root user access only. The framework's folder should be configured so only your web server will have rights to read the files in it. This policy is to prevent the unwanted direct access to framework files by the request to web server. When you create your application, you also configure its real path to the Yii directory.
In short, for Linux systems with a web server running as the user apache
, you can install Yii Version 1.1.13. e9e4a0 from the command line as follows:
# cd /var/local/ # wget http://yii.googlecode.com/files/yii-1.1.13.e9e4a0.tar.gz # tar –zxf yii-1.1.13.e9e4a0.tar.gz && rm yii-1.1.13.e9e4a0.tar.gz # chown -R apache yii-1.1.13.e9e4a0
Note
Note that for working in a system folder like /var/local
, you need to be a root user. Of course, you can use any directory you like.
Inside the resulting yii
directory, you'll find three folders named as follows:
demos
framework
requirements
The requirements
folder is a helper to make a self test for your server setup. You can copy it to your webroot folder and access http://your.website.address/requirements/index.php
from the web browser to see whether your system complies to the framework's requirements.
The framework
folder is the Yii framework itself. You essentially only need its contents to use Yii in your application. From now on, we'll refer to the path under which Yii is installed as path-to-yii
.
Once you have the framework installed on your machine, there is a command-line tool called yiic
that is capable of building the skeleton application, among other quite numerous things. Open a console, navigate to your webroot, then run the autogenerator command:
$ path-to-yii/framework/yiic webapp sitename
The sitename
token is the path to your new application. As you are already in your webroot
folder, you just need to provide a name of a subfolder and not a full path to it.
This shell script is intended for use on Linux, so if you are on Windows, there is a yiic.bat
file. Both scripts essentially run framework/yiic.php
with the PHP command-line executable on your system, so the alternative method is to run it as follows:
$ php path-to-yii/framework/yiic.php webapp sitename
Before you can visit your site, you must check the index.php
file to make sure it points to the actual Yii installation folder.
Make sure you have a line that looks like this in sitename
/index.php
:
$yii='path-to-yii/framework/yii.php';
You can now visit your site and see the basic hello world website with a homepage, about, contact, and a login page. As the application was created in a subfolder of your webroot
folder, the URL should be like this: http://your.website.address/sitename
.
Your application folder will contain the following structure under the sitename
folder:
assets
: Contains published resource filescss
: Contains CSS filesimages
: Contains image filesthemes
: Contains application themesprotected
: Contains protected application files
The protected folder contains the following directories:
commands
: Contains customized yiic commandscomponents
: Contains reusable user componentsconfig
: Contains configuration filescontrollers
: Contains controller class filesdata
: Contains the sample databaseextensions
: Contains third-party extensionsmessages
: Contains translated messagesmodels
: Contains model class filesruntime
: Contains temporarily generated filestests
: Contains test scriptsviews
: Contains controller view and layout files
The views
folder contains the following directories:
layouts
: Contains layout view filessite
: Contains view files for the site controller