Downloading CakePHP
https://github.com/cakephp/cakephp/tagsOR
http://cakephp.org/
download the file and place it in working folder ie,
in windows
drive/wamp/www/place_folder(eg: cakephp)
in linux
/opt/lampp/htdocs/cakephp
Naming Conventios
Table names are plural and lowercased .model names are singular and CamelCased: ModelName, model filenames are singular and underscored: model_name.php .
controller names are plural and CamelCased with *Controller* appended: ControllerNamesController, controller filenames are plural and underscored with *controller* appended: controller_names_controller.php .
Give permission to folder
HTTPDUSER=`ps aux | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\ -f1`
setfacl -R -m u:${HTTPDUSER}:rwx app/tmp
setfacl -R -d -m u:${HTTPDUSER}:rwx app/tmp
Getting Start with CakePHP
folder structure
/cakephp(my_foler_name)
/app
/congig
/controller
/model
/view
/temp
/lib //less folder only given
/lib
/plugins
/vendors
.htaccess
index.php
README
CakePHP database configration
/app/Config/database.php.default. Make a copy of this file in
the same directory, but name it database.php.
cakephp/app/Config/database.php (if database.php is not there create new file)
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost', //hostname
'port' => '',
'login' => 'root', //username
'password' => '', //password
'database' => 'cakephp', //database name
'schema' => '',
'prefix' => '',
//'encoding' => 'utf8',
);
Creating database
First we will create a table in our database for example:CREATE TABLE `categories` ( `id` INTEGER(11) NOT NULL AUTO_INCREMENT, `name` VARCHAR(255) NOT NULL, PRIMARY KEY (`id`)) ENGINE=MyISAM>> table names are in plural Creating Model for table
1. create a model for the table `categories`2. path >> /app/model/Category.phpExample code<?phpclass Category extends AppModel { var $name = 'Category';}?>>> naming convention for models is that file and class names are in singular explanation:extended CakePHP’s AppModel class and declared a variable $name which will be used from our Controller to access the model’s functions.
Creating Controller
1. path >> /app/controllers/CategoriesController.php
Example code<?php
class CategoriesController extends AppController {
var $name = 'Categories'; public $helpers = array('Html', 'Form');
function index() {
$this->set('categories', $this->Category->find('all'));
}
}
?> explanation:>> We are extending AppController class, declaring $name variable.>> added function called index(). The controller functions are called actions. >> 'categories' table name>> By defining function index() in our CategoriesController,
users can now access the logic there by requesting
www.example.com/categories/index or in localhost >> localhost/categories/index>> $this->set('categories', $this->Category->find('all')); This is to selecting all categories from categories table and assigning the
resulting array to categories variable. This variable will be available
in our index view >> plural controller names. Creating routes
create readable, understandable action names. You can map URLs to your code
Creating View
create folder Categories this folder name controller name case sensitive. 1. path >> /app/views/Categories/index.ctp2. Create a file index.ctp in /app/views/Categories/index.ctp , Ctp(CakeTemplatePHP) files are CakePHP template files. references http://book.cakephp.org/2.0/en/views.html#view-templates Example code<h1>Categories</h1><?php echo $this->Html->link('Add Category',array('controller' => 'categories', 'action' => 'add')); ?> <table> <tr> <th>Id</th> <th>Title</th> </tr> <?php foreach ($categories as $category): ?> <tr> <td><?php echo $category['Category']['id']; ?></td> <td> <?php echo $this->Html->link($category['Category']['name'], array('controller' => 'categories', 'action' => 'view', $category['Category']['id'])); ?> </td> </tr> <?php endforeach; ?></table> $html This code generates a link to /categories/view/category_id
to run file http://localhost/cakephp/categories
Creating Add category
create add.ctp file inside path >> /app/views/Categories/index.ctp <!-- File: /app/views/categories/add.ctp --> <h1>Add Category</h1><?phpecho $this->form->create('Category');echo $this->form->input('name');echo $this->form->end('Save Post');?>create() --> If create() is called with no parameters supplied, it assumes you are building a form that submits to the current controller’s add() action (or edit() action when id is included in the form data), via POST.$form->input() --> used to create form elements of the same name.
> The first parameter tells which field they correspond to
> second parameter allows to specify a wide array of options(number of rows for the textarea)
echo $this->Form->input('password', array('type'=>'password')); $form->end() --> call generates a submit button and ends the form
in controller
<?php
class CategoriesController extends AppController {
var $name = 'Categories';
public $helpers = array('Html', 'Form');
function index() {
$this->set('categories', $this->Category->find('all'));
}
function add() {
if (!empty($this->data)) {
if ($this->Category->save($this->data)) {
$this->Session->setFlash('Your category has been saved.');
$this->redirect(array('action' => 'index'));
}
}
}
}
?>
class CategoriesController extends AppController {
var $name = 'Categories';
public $helpers = array('Html', 'Form');
function index() {
$this->set('categories', $this->Category->find('all'));
}
function add() {
if (!empty($this->data)) {
if ($this->Category->save($this->data)) {
$this->Session->setFlash('Your category has been saved.');
$this->redirect(array('action' => 'index'));
}
}
}
}
?>
add in to view/Category/index.cpt
<?php echo $this->Html->link('Add Category',array('controller' => 'categories', 'action' => 'add')); ?>the link to add category http://localhost/cakephp/categories
No comments:
Post a Comment