title: How to build a pagination API in Yii 1.x using CPagination
body: <p>I'm new to yii. Currently I am building an API for some project. I managed to get all data from the database and sent as a response to the front end. Now there is an enhancement that I should limit the results outputted per page. So Pagination. I read a lot on CPagination in yii and no matter what I tried I'm getting a 500 internal server error. This is my codes;</p>
  
  <pre><code>    public function actionIndex()
  {
      //getting page number from front end
      $request = file_get_contents('php://input');
  
      if (is_null($request)) {
          $page = 1;
      } else {
          $input = json_decode($request, true);
          $page = $input['page'];
      }
  
      //Criteria sorting templates by id DESC
      $criteria = new CDbCriteria;
      $criteria-&gt;order = 'id DESC';
  
      //Number of rows returned
      $count = TblTemplate::model()-&gt;count($criteria);
  
      //Templates per page
      $perPage = 2;
  
      //Calculating the offset
      $offset = ($page &gt; 1) ? ($page * $perPage) - $perPage : 0;
  
      $pages = new CPagination($count);
      $pages-&gt;pageSize = $perPage;
      $pages-&gt;offset   = $offset;
      $pages-&gt;applyLimit($criteria);
  
      //fetching all templates    
      $templates = TblTemplate::model()-&gt;findAll($criteria);
  
      $response = [];
  
      //Fetching all array - filtering through the cluster
      foreach ($templates as $template) {
          $response[] = [
              'id'      =&gt; $template-&gt;id,
              'name'    =&gt; $template-&gt;name,
              'email'   =&gt; $template-&gt;email,
              'content' =&gt; $template-&gt;content 
          ];
      }
  
      echo json_encode($response);
  }
  </code></pre>
  
  <p>Thanks for your help.</p>