Book Image

Instant Yii 1.1 Application Development Starter

Book Image

Instant Yii 1.1 Application Development Starter

Overview of this book

PHP is one of the most popular languages for building web sites and applications. While the Yii framework came a little bit later to the PHP scene, it has taken a lot of the best practices from around the Web and put it all into a single, awesome framework for making custom web apps. Yii 1.1 Application Development Starter is a straightforward, hands-on look at what is rapidly becoming one of the most revered frameworks for custom web application development. The Yii framework follows a tried and true object-oriented software development pattern called Model-View-Controller, making it quick and easy to build database driven applications. This book takes you through everything you need to know to get set up and rolling with the Yii framework. You will learn how to plan and start your application and how to take it where you want to go with the tools and extensions available with the Yii framework. This book will teach you how to build an application from the ground up, how to make sure you have everything you need in your hosting environment and get the Yii framework installed, how to create a strong relational database design, and how to set up model classes for your tables. You will also learn how to generate CRUD code to add/remove, view, and list records from your tables, then add custom routes, widgets, and extensions to make a robust application. Additionally, you will learn how to integrate authentication and role-based access permissions throughout your site. With this book, you will learn everything you need to get started with web application development using the Yii PHP framework.
Table of Contents (7 chapters)

Installation


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.

Server requirements

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.

Step 1 – Setting up Yii

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.

Step 2 – Creating your project

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.

And that's it!!

Your application folder will contain the following structure under the sitename folder:

  • assets: Contains published resource files

  • css: Contains CSS files

  • images: Contains image files

  • themes: Contains application themes

  • protected: Contains protected application files

The protected folder contains the following directories:

  • commands: Contains customized yiic commands

  • components: Contains reusable user components

  • config: Contains configuration files

  • controllers: Contains controller class files

  • data: Contains the sample database

  • extensions: Contains third-party extensions

  • messages: Contains translated messages

  • models: Contains model class files

  • runtime: Contains temporarily generated files

  • tests: Contains test scripts

  • views: Contains controller view and layout files

The views folder contains the following directories:

  • layouts: Contains layout view files

  • site: Contains view files for the site controller