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

Using exceptions


Exceptions are a core PHP feature, but they are seldom used fairly. Yii makes exceptions very useful.

There are two main areas where Yii exceptions come in handy, which are as follows:

  1. Exceptions allow simplifying the process of detecting and fixing application errors and special situations, such as database connection failure or API failure.

  2. Exceptions allow generating different HTTP responses in a very clean way.

Generally, an exception should be thrown when a component cannot handle a special situation, such as the one said earlier, and needs to leave it to higher-level components.

How to do it…

  1. Let's assume that we have an application/apis/lyrics/LyricsFinder.php class that makes an HTTP request to an API using CURL and returns song lyrics based on its name. This is how we can use exceptions inside of it:

    // create some custom exceptions to be able to catch them
    // specifically if needed
    
    // general lyrics finder exception
    class LyricsFinderException extends CException {}
    
    // used when there is a connection problem
    class LyricsFinderHTTPException extends LyricsFinderException{}
    
    
    class LyricsFinder
    {
       private $apiUrl = 'http://example.com/lyricsapi&songtitle=%s';
    
       function getText($songTitle)
       {
          $url = $this->getUrl($songTitle);
          $curl = curl_init();       
          curl_setopt($curl, CURLOPT_URL, $url); 
          curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
          $result = curl_exec($curl);
    
          // if there is an HTTP error, we'll throw an exception if($result===false)
          {
             $errorText = curl_error($curl);
             curl_close($url);
             throw new LyricsFinderHTTPException($errorText);
          }
    
          curl_close($curl);
          return $result;
       }
       
       private function getRequestUrl($songTitle)
       {
          return sprintf($this->apiUrl, urlencode($songTitle));
       }
    }
  2. As we don't know how a specific application needs to handle its API connection, we will leave it to the application itself by throwing a custom LyricsFinderHTTPException. This is how we can handle it in our protected/controllers/TestController.php:

    class TestController extends CController
    {
      public function actionIndex($song)
      {
        $lyric = 'Nothing was found.';
        
        // importing api class
        Yii::import('application.apis.lyrics.LyricsFinder');
    
        $finder = new LyricsFinder();
    
        if(!empty($song))
        {
           // We don't want to show user an error.
           // Instead we want to apologize and
           // invite him to try again later.
           try {
              $lyric = $finder->getText($song);
           }
           // we are looking for specific exception here
           catch (LyricsFinderHTTPException $e)
           {
              echo 'Sorry, we cannot process your request. Try again later.';
           }
        }         
    
        echo $lyric;
      }
    }
  3. Another usage of Yii exceptions is the generation of different HTTP responses by throwing CHttpException. For example, an action that displays a blog post represented by a Post model, loaded by its ID will look like this:

    class PostController extends CController
    {
      function actionView()
      {
        if(!isset($_GET['id']))
          // If there is no post ID supplied, request is definitely wrong.
          // According to HTTP specification its code is 400.
          throw new ChttpException(400);
          
          // Finding a post by its ID
          $post = Post::model()->findByPk($_GET['id']);
          
          if(!$post)
             // If there is no post with ID specified we'll generate
             // HTTP response with code 404 Not Found.
             throw new CHttpException(404);
    
             //  If everything is OK, render a post
             $this->render('post', array('model' => $post));
      }
    }

How it works…

Yii converts all non-fatal application errors to CException automatically.

Additionally, the default exception handler raises either the onError or an onException event. The default event handler writes a log message with error level set to error. Additionally, if your application's YII_DEBUG constant is set to true, unhandled exception or error will be displayed at a handy error screen. This screen includes a call stack trace, a code area where the exception was raised, and the file and line where you can look for the code to fix.