Book Image

Yii 1.1 Application Development Cookbook

Book Image

Yii 1.1 Application Development Cookbook

Overview of this book

When Alex told me he was about to write a Yii cookbook about a year ago, I was wondering how original it would be, considering the fact that there was already an online user-contributed cookbook (aka. Yii wiki). It turned out Alex produced a book that is not only full of wisdom about how to use Yii effectively, but also presented in such a systematic way that it can be taken as an essential companion book to the definitive guide to Yii. In fact, Alex has successfully intrigued the interest of every member in the Yii developer team when he asked for review and comments on his newly finished book chapters.As the founder and the lead developer of the Yii framework, I feel this book is a must-read for every Yii programmer. While this book does not describe directly the rules set by Yii, it shows how to program with Yii from a practical perspective. People who are driven by tight project schedules will find this book very handy as it gives ready-to-use solutions to many problems they may face in their projects; people who are already familiar with Yii will also find this book very informative as most problem solutions given in the book can be considered as officially recommended because they have undergone thorough review of every Yii developer team member. Alex, through this book and his active participation in the Yii project, proved himself to be a great programmer as well as a good writer. Qiang XueLead developer of the Yii framework Yii framework is a rapidly growing PHP5 MVC framework often referred to as Rails for PHP. It has become a solid base for many exciting web applications such as Stay.com and Russia Today's meetfriends.rt.com and can be a good base for your developments. Yii is an object-oriented, high-performance, component-based PHP web application framework. Yii is pronounced as Yee and is an acronym for "Yes It Is!". Familiar with Yii and want to exploit it to its full potential, but do not know how to go about it? Yii 1.1 Application Development Cookbook will show you how to use Yii efficiently. You will learn about implementing shortcuts using core features, creating your own reusable code base, using test-driven development, and many more topics that will escalate your knowledge in no time at all! Yii 1.1 Application Development Cookbook will help you learn more about Yii framework and application development practices in general with demonstrations of shortcuts and information about dangerous things you should not do. Grouped in 13 chapters, the recipes will assist you to write your applications exploiting Yii core functionality to its full potential. The chapters are generally independent of each other and you can start reading from the chapter you need most, whether it is "AJAX and jQuery", "Database, Active Record and Model Tricks" or "Extending Yii". The most interesting topics include Yii application deployment, a guide to writing your own extensions, advanced error handling, debugging and logging, application security, and performance tuning. Yii 1.1 Application Development Cookbook will help you utilize Yii functionalities completely and efficiently.
Table of Contents (21 chapters)
Yii 1.1 Application Development Cookbook
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Configuring components


Yii is a very customizable framework. Moreover, as in every customizable code, there should be a convenient way to setup different application parts. So in Yii, this is provided through a configuration file named main.php located at protected/config/.

How to do it…

If you have worked with Yii before, then you have probably configured a database connection:

return array(
   …
   'components'=>array(
      'db'=>array(
         'class'=>'system.db.CDbConnection',
         'connectionString'=>'mysql:host=localhost;dbname=database_name',
         'username'=>'root',
         'password'=>'',
         'charset'=>'utf8',
      ),
      …
   ),
   …
);

This way of configuring component is used when you want to use a component across all application parts. With the preceding configuration, you can access a component by its name, such as Yii::app()->db.

How it works…

When you are using the Yii::app()->db component for the first time directly or through active record model, Yii creates a component and initializes its public properties with the corresponding values provided in db array under the components section of the main.php application configuration file. In the preceding code, 'connectionString' the value will be assigned to CDbConnection::connectionString, 'username' will be assigned to CDbConnection::username, and so on.

If you want to find out what 'charset' stands for or want to know what else you can configure in the db component, then you need to know its class. In case of db component, the class is CDbConnection. You can refer to its API page at http://www.yiiframework.com/doc/api/CDbConnection/ and look for its public properties you can set from config.

In the preceding code, the 'class' property is a bit special because it is used to specify component class name. It does not exist in the CDbConnection class. Therefore, it can be used to override a class as follows:

return array(
   …
   'components'=>array(
      'db'=>array(
         'class'=>'application.components.MyDbConnection',
         …
      ),
      …
   ),
   …
);

This way, you can override each application component and it is very useful whenever a standard component does not fit your application.

There's more...

Now, let's find out which standard Yii application components you can configure. There are two application types bundled with Yii which are as follows:

  1. Web application (CWebApplication)

  2. Console application (CConsoleApplication)

Both are extended from CApplication, so both console and web applications are sharing its components.

You can get the component names from API pages (http://www.yiiframework.com/doc/api/) and the source code of the registerCoreComponents application method, but let's list them here so the list can be used as a reference.

Both console and web application components are listed in the following table:

Component name

Default/suggested component class

Description

coreMessages

CPhpMessageSource

This component provides the source for translating Yii framework messages.

db

CDbConnection

This component provides a database connection.

messages

CPhpMessageSource

This component provides the source for translating application messages.

errorHandler

CErrorHandler

This component handles PHP errors and uncaught exceptions.

securityManager

CSecurityManager

This component provides security-related services, such as hashing, encryption, and so on.

statePersister

CStatePersister

This component provides global state persistence methods.

format

CFormatter

This component provides a set of commonly used data formatting methods.

cache

CFileCache

This component provides a caching feature.

Additional components available only for web application are listed in the following table:

Component name

Default component class

Description

session

CHttpSession

This component provides the session-related functionalities.

request

CHttpRequest

This component encapsulates the $_SERVER variable and resolves its inconsistency among different web servers.

It also manages the cookies sent from and to the user.

urlManager

CUrlManager

URL router; used both to generate and resolve application URLs.

assetManager

CAssetManager

This component manages the publishing of private asset files.

user

CWebUser

This component represents the user session information.

themeManager

CThemeManager

This component manages themes.

authManager

CPhpAuthManager

This component manages role-based access control (RBAC).

clientScript

CClientScript

This component manages client scripts (JavaScript and CSS).

widgetFactory

CWidgetFactory

This component creates widgets and supports widget skinning.

You can add your own application components (classes extended from CComponent) by simply adding new configuration items and pointing their class properties to your custom classes.

See also

  • The recipe named Configuring widget defaults in this chapter