CAKEPHP: Json Output

Here is an easy way to output your JSON from CakePHP version 1.3.10




First let say we create sql table from query like so.

CREATE TABLE contacts (id int(6) NOT NULL auto_increment,first varchar(15) NOT NULL,last varchar(15) NOT NULL,phone varchar(20) NOT NULL,mobile varchar(20) NOT NULL,fax varchar(20) NOT NULL,email varchar(30) NOT NULL,web varchar(30) NOT NULL,PRIMARY KEY (id),UNIQUE id (id),KEY id_2 (id))


In which we ought to get a table name contacts with above fields.
We generate the models. So it will be something like this with validations.

<?php
class Contact extends AppModel {
    var $name = 'Contact';
    var $displayField = 'email';
    var $validate = array(
        'first' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'last' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'phone' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'mobile' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'fax' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'email' => array(
            'email' => array(
                'rule' => array('email'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'web' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
    );
}
Now we create the Controller
<?php
class ContactsController extends AppController {

    var $name = 'Contacts';
    function index() {
            $this->set('result',$this->Contact->find('all',array (
                        'recursive' => 1
                    )));
            $this->layout = 'json';
    }
}
?>
Create a file cakephp_root/app/views/layout/json.ctp

This should match the name at your $this->layout = 'json'; with the extension ctp. Inside just create something like below
<?php echo $content_for_layout; ?>

So now lets create a demo data inside with this SQL queries
INSERT INTO `isq`.`contacts` (`id`, `first`, `last`, `phone`, `mobile`, `fax`, `email`, `web`) VALUES (NULL, 'John', 'Wilson', '0312345678', '0123456789', '0323456789', 'john_wilson@demo.com', 'http://www.john_wilson.com'), (NULL, 'Mary', 'Queresma', '0234567890', '0191234567', '0234567891', 'marymary@demo.com', 'http://www.marryme.com');
Table view will be as below


So now lets look at the view name index.ctp in app/view/contacts. The only codes that we shall insert is
<?php
echo json_encode($result);
?>

Go to http://web_domain/cakephp/contacts and you should get something like this.

[{"Contact":{"id":"1","first":"John","last":"Wilson","phone":"0312345678","mobile":"0123456789","fax":"0323456789","email":"john_wilson@demo.com","web":"http:\/\/www.john_wilson.com"}},{"Contact":{"id":"2","first":"Mary","last":"Queresma","phone":"0234567890","mobile":"0191234567","fax":"0234567891","email":"marymary@demo.com","web":"http:\/\/www.marryme.com"}}]
All done. The json from the database. You can download the example files at here. DOWNLOAD

Comments

Popular Posts