Book Image

Yii Application Development Cookbook - Second Edition - Second Edition

Book Image

Yii Application Development Cookbook - Second Edition - Second Edition

Overview of this book

The Yii framework is a rapidly growing PHP5 MVC framework often referred to as Rails for PHP. It has already become a solid base for many exciting web applications such as Stay.com and can be a good base for your developments, too. This book will help you to learn Yii quickly and in more depth for use in for your developments."Yii Application Development Cookbook" will show you how to use Yii efficiently. You will learn about taking shortcuts using core features, creating your own reusable code base, using test driven development, and many more topics that will give you a lot of experience in a moderate amount of time.The second edition fixes all errata found in the first edition and also features new recipes on the client side, HTTP caching, and using Composer with Yii.The chapters of the book are generally independent and since this book's goal is to enhance a practical approach to Yii development, you can start reading from the chapter you need most, be it Ajax and jQuery, Database, Active Record, and Model Tricks, or Extending Yii."Yii Application Development Cookbook" will help you to learn more about the Yii framework and application development practices in general, showing shortcuts and dangerous things you shouldn't do.With all the recipes grouped in 13 chapters, you will write your applications more efficiently using shortcuts and using Yii core functionality in a good way. The most interesting topics are; Yii application deployment, a guide to writing your own extensions, advanced error handling, debugging and logging, application security, performance tuning, and much more."Yii Application Development Cookbook" will help you to learn more about the Yii framework and application development practices in general. You will write your applications more efficiently using shortcuts and using Yii core functionality in a good way.
Table of Contents (20 chapters)
Yii Application Development Cookbook Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Working with requests


You can work with request data directly using PHP superglobals such as $_SERVER, $_GET, or $_POST but the better way is to use Yii's powerful CHttpRequest class that resolves inconsistencies among different web servers, manages cookies, provides some additional security, and has a nice set of OO methods.

How to do it…

You can access the request component in your web application by using Yii::app()->getRequest(). So, let's review the most useful methods and their usage, methods that return different parts of the current URL. In the following table, the returned parts are marked with a bold font.

Methods

Results

getUrl

http://cookbook.local/test/index?var=val

getHostInfo

http://cookbook.local/test/index?var=val

getPathInfo

http://cookbook.local/test/index?var=val

getRequestUri

http://cookbook.local/test/index?var=val

getQueryString

http://cookbook.local/test/index?var=val

The methods that allow us to ensure the request types are getIsPostRequest, getIsAjaxRequest , and getRequestType .

  • For example, we can use getIsAjaxRequest to serve different content based on the request type:

    class TestController extends CController
    {
       public function actionIndex()
       {
          if(Yii::app()->request->isAjaxRequest)
             $this->renderPartial('test');
          else
             $this->render('test');
       }
    }

    In the preceding code, we are rendering a view without a layout if the request is made through AJAX.

  • While PHP provides superglobals for both POST and GET, Yii allows us to omit some additional checks:

    class TestController extends CController
    {
       public function actionIndex()
       {
          $request = Yii::app()->request;
    
          $param = $request->getParam('id', 1);
          // equals to
          $param = isset($_REQUEST['id']) ? $_REQUEST['id'] : 1;
    
          $param = $request->getQuery('id');
          // equals to
          $param = isset($_GET['id']) ? $_GET['id'] : null;
    
          $param = $request->getPost('id', 1);
          // equals to
          $param = isset($_POST['id']) ? $_POST['id'] : 1;
       }
    }
  • getPreferredLanguage tries to determine the user's preferred language. It can't be completely accurate, but it is good to use it as a fallback in case the user has not specified a preferred language manually.

    class TestController extends CController
    {
       public function actionIndex()
       {
          $request = Yii::app()->request;
          $lang = $request->preferredLanguage;
    
          // Trying to get language setting from 
          // Settings table that holds id of the user,
          // setting name and setting value.
          $criteria = new CDbCriteria();
          $criteria->compare('user_id', $request->getQuery('userid'));
          $criteria->compare('key', 'language');
          $setting = Settings::model()->find($criteria);
          if($setting)
             $lang = $setting->value;
    
          Yii::app()->setLanguage($lang);
          
          echo Yii::t('app', 'Language is: ').$lang;
       }
    }
  • sendFile allows us to initiate a file download as follows:

    class TestController extends CController
    {
       public function actionIndex()
       {
       $request = Yii::app()->getRequest();
          $request->sendFile('test.txt', 'File content goes here.');
       }
    }

    This action will trigger a file download and send all necessary headers, including content type (mimetype) and content length. The MIME type, if not set manually as a third parameter, will be guessed based on the filename's extension.

  • The last thing we are going to show in this chapter is the getCookies method. It returns a CCookieCollection class instance that allows us to work with cookies. As CCookieCollection extends CMap, we can use some native PHP methods as follows:

    class TestController extends CController
    {
       public function actionIndex()
       {
          $request = Yii::app()->request;
          // getting a cookie
          $cookie = $request->cookies['test'];
          if($cookie)
             // printing cookie value
             echo $cookie->value;      
          else {
             // creating new cookie
             $cookie=new CHttpCookie('test','I am a cookie!');
             $request->cookies['test'] = $cookie;
          }
    }

There's more...

If you are working with a lot of cookie values and want to shorten the code provided, then you can use a helper as follows:

class Cookie
{
  public static function get($name)
  {
       $cookie=Yii::app()->request->cookies[$name];
       if(!$cookie)
           return null;

       return $cookie->value;
  }

  public static function set($name, $value, $expiration=0)
  {
       $cookie=new CHttpCookie($name,$value);
       $cookie->expire = $expiration;
       Yii::app()->request->cookies[$name]=$cookie;
  }
}

After you drop this code into protected/components/Cookie.php, you will be able to perform the following:

class TestController extends CController
{
   public function actionIndex()
   {
      $cookie = Cookie::get('test');
      if($cookie)
         echo $cookie;      
      else
         Cookie::set('test','I am a cookie!!');
   }
}