Wednesday, May 31 2017
In this post explains, how to implement CRUD operations in CakepPHP 2.x version. we are using Cakephp 2.9.3v. First download the cakephp 2.9.3v source files from Github here. Its useful for a beginner to start cakephp.
We aren’t implement jquery validations and datatable too. We use only default cakephp libraries to validate the user’s input with bootstrap 3. We use most of form fields and cover all possible cases to validate it.
See our demo video on Youtube:
Cakephp folder structure is same as other frameworks but difference in assets folder.
Step 1:
Download cakephp version from here.
Step 2:
To update CakePHP Debug Level in config/core.php
Configure::write('debug', 2);
Production Mode:
0: No error messages, errors, or warnings shown. Flash messages redirect.
Development Mode:
1: Errors and warnings shown, model caches refreshed, flash messages halted.
2: As in 1, but also with full debug messages and SQL output.
Step 3:
To update security salt with security hashing methods in config/core.php
Configure::write('Security.salt', 'YOUR_RANDOM_KEY');
Step 4:
Update your database connection settings in config/database.php
public $default = array( 'datasource' => 'Database/Mysql', 'persistent' => false, 'host' => 'HOSTNAME', 'login' => 'YOUR_USERNAME', 'password' => 'YOUR_PASSWORD', 'database' => 'YOUR_DATABASE_NAME', 'prefix' => '', 'encoding' => 'utf8', );
Step 5:
For a mail settings to rename email.php.default file to email.php and get a backup of email.php.default file.
Step 6:
Custom url rewriting and also we can able to add define constants variables to access globally in config/routes.php
Step 7:
Setup controller file Controller/AppController.php
update your helpers/components functions if needed.
Create a controller
class UsersController extends AppController { public $helpers = array('Html', 'Form', 'Flash'); public $components = array('Flash'); public $uses = array('User', 'Country', 'State', 'City'); //tables are uses in this controller. public function index() { $this->paginate = array( 'conditions' => array('status' => '1'), 'limit' => 3, 'order' => array('id' => 'desc') ); $users = $this->paginate('User'); $this->set('users', $users); } }
$this->set uses to get the results from controller to view file.
we can multiple values in array too.
$data = array( 'user_id' => $user_id, 'username' => $username, 'result' => $results ); $this->set('data', $data);
Step 8:
Create a sample model file
<?php class Country extends AppModel { var $name = "Country"; }
var $name = TABLE_NAME;
Step 9:
View folder has Elements, Email, Errors, Helper, Layouts, Pages, and Scaffolds these are the basic folders are generated while installation of cakephp.
Elements:
In this folder, we move common files like header, footer and sidebar files to access those files in globally.
Syntax: $this->Element('Header')
If you want to create a custom file means, create a folder for it named it as module name like “Users”.
Inside this folder create add.ctp file. In cakephp we have to save all the view files are “.ctp” format.
Now we have implemented CRUD functionalities by using AdminLTE theme.
Output:
Source code: Download from Github Here