Wednesday, October 26, 2011

Simple Pagination In Cakephp

Achieving Pagination in Cakephp is Slight simple. We use the initial method for applying the pagination.Here i use the Helper to load a paginate class and its instantiate part by using $paginate variable in Controller.
For this first up all, we define the Helper variable like, 



var $name='news';//init for news.
var $helpers=array('Html','Session'); // Load the Helpers to an array.
var $paginate=array('limit' =>5, 'order'=> array('News.id'=>'asc')); //init for Paginate and its options.

// Then we make the Controller - newsController - Code

function index(){
$data=$this->$paginate('News'); 
//Apart from find(); function we use the paginate();
$this->set('news',$data);
} 

In View Page (views/news/index.ctp page), we add the following code for display the data with Pagination. 

echo $html->css('default'); //  if need to load Style sheet.
 
echo $html->link(
    'Add New News',
    array(
      'controller' => 'News',
      'action'     => 'add',
    ),
    array(
     
    ),
    null
);
 

echo $html->tableHeaders(
    array(
      'ID',
      'Title',
      'News',
      'Comments'
    )
  );

foreach($news as $news)
{
  echo $html->tableCells(
      array(
        array(
          $news['News']['id'],
          $news['News']['n_title'],
          $news['News']['n_news'],
          $news['News']['n_comments']
        )
      )
    );
}
 
 

echo $html->div(
  null,
  $paginator->prev(
    '<< Previous',
    array(
      'class' => 'PrevPg'
    ),
    null,
    array(
      'class' => 'PrevPg DisabledPgLk'
    )
  ).
  $paginator->numbers().
  $paginator->next(
    'Next >>',
    array(
      'class' => 'NextPg'
    ),
    null,
    array(
      'class' => 'NextPg DisabledPgLk'
    )
  ),
  array(
    'style' => 'width: 100%;'
  )
);  
It displays the data with Pagination. Displays first 5 rows as per set the limit by 5.

Tuesday, October 25, 2011

Cakephp Basic Steps : Need To Understand By Beginers.

Understanding CakePhp Structure

Cakephp using basic MVC Architecture and it resolves the structural forms (eg. Form Handling) in new Version 1.3.



Models :-
Models represent data and are used in CakePHP applications for data access. Used for Communicating View and Controller. (Ex. Database Connection,Table,Query Dealings).

Controller :-
A controller is used to manage the logic for a part of your application. Most commonly, controllers are used to manage the logic for a single model.It represents the Core Part of the MVC.

Views :- Views are responsible for generating the specific output required for the request. Often this is in the form of HTML, XML, or JSON, but streaming files and creating PDF's that users can download are also responsibilities of the View Layer.

Apart from this basic Architecture, we need more tools to create a MVC World, like

Components :-Components are packages of logic that are shared between controllers. If you find yourself wanting to copy and paste things between controllers, you might consider wrapping some functionality in a component.
CakePHP also comes with a fantastic set of core components you can use to aid in:
  • Security
  • Sessions
  • Access control lists
  • Emails
  • Cookies
  • Authentication
  • Request handling
This represents the Controller Extensions.
A Component is a class that aids in controller logic.
If you have some logic you want to share between controllers (or applications), a component is usually a good fit.

Controllers are also fitted with callbacks. These callbacks are available for your use, just in case you need to insert some logic between CakePHP’s core operations. Callbacks available include:

  • beforeFilter(), executed before any controller action logic
  • beforeRender(), executed after controller logic, but before the view is rendered
  • afterFilter(), executed after all controller logic, including the view render. There may be no difference between afterRender() and afterFilter() unless you’ve manually made a call to render() in your controller action and have included some logic after that call.
View Extensions ("Helpers")

A Helper is a class that aids in view logic. Much like a component used among controllers, helpers allow presentational logic to be accessed and shared between views. One of the core helpers, AjaxHelper, makes Ajax requests within views much easier.

Model Extensions ("Behaviors")

Similar to Components and Helpers, "Behaviors" work as ways to add common functionality between models. For example, if you store user data in a tree structure, you can specify your "User" model as behaving like a tree, and gain free functionality for removing, adding, and shifting nodes in your underlying tree structure.

Just like controllers, models are featured with callbacks as well:

  • beforeFind()
  • afterFind()
  • beforeValidate()
  • beforeSave()
  • afterSave()
  • beforeDelete()
  • afterDelete()
A Typical CakePHP Request




File and Classname Conventions

In general, filenames are underscored while classnames are CamelCased. So if you have a class MyNiftyClass, then in Cake, the file should be named my_nifty_class.php. Below are examples of how to name the file for each of the different types of classes you would typically use in a CakePHP application:
  • The Controller class KissesAndHugsController would be found in a file named kisses_and_hugs_controller.php (notice _controller in the filename)
  • The Component class MyHandyComponent would be found in a file named my_handy.php
  • The Model class OptionValue would be found in a file named option_value.php
  • The Behavior class EspeciallyFunkableBehavior would be found in a file named especially_funkable.php
  • The View class SuperSimpleView would be found in a file named super_simple.php
  • The Helper class BestEverHelper would be found in a file named best_ever.php