Home  • Framework • CakePHP

Getting Started The CakePHP

getting started cakePHP Step 1: Download the required version of CakePHP that you need. Step 2: Extract it to your web server (htdocs or www folder) and rename it as your project name. Explore that folder and it should contains as the following folders and files: 1. config 2. contrib ... Step 3: Open the file config/config.php and change as follows: Step 4: Change the value of Security.salt at Line no.225. Change the value of Security.cipherSeed at Line no.230. Step 5: Rename database.php.default file to database.php at the app/Config/ directory. Step 6: Run the project URL(http://localhost/cakephp/) at the browser.

CakePHP Naming Conventions

Controller Conventions – Controller class names are plural, CamelCased and end in Controller.(PostsController, LatestPostsController) Model Conventions – Model class names are singular and CamelCased.(Post, LatestPost) Database Conventions – Table names corresponding to CakePHP models are plural and underscored. (posts, latest_posts) View Conventions – View template files are named after the controller functions they displayed, in an underscored form. The postDetails() function of PostController class will look for a view template in app/View/Post/post_details.ctp. The basic pattern is app/View/Controller/underscored_function_name.ctp

Sample Project

In this sample project we will create a products table at the cakephp_db database. And we will insert some data manually at this table. We will fetch and display products in our sample CakePHP project. Table Creation & Data Insert: Following SQL is used for products table creation.
CREATE TABLE `products` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
 `description` text COLLATE utf8_unicode_ci NOT NULL,
 `price` float(10,2) NOT NULL,
 `created` datetime NOT NULL,
 `modified` datetime NOT NULL,
 `status` tinyint(1) NOT NULL DEFAULT '1',
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
Once table creation is completed, insert some demo product data into this table. Controller: Create a products controller with ProductsController class into the app/Controller/ directory. Controller file and class name should be ProductsController.
<?php
class ProductsController extends AppController {
    public function index() {
        //fetch products resultset from databse
        $products = $this->Product->find('all',array('fields'=>array('Product.id','Product.title','Product.description','Product.price','Product.created','Product.status'),'conditions'=>array('Product.status'=>1)));

        //set products data and pass to the view 
        $this->set('products',$products);
    }
}
View: Create view for display products in app/View/ directory. The controller class name is ProductsController and method is index. For creating the index view, we need to Products/ directory and a index.ctp file. So, the complete path of the view file (index.ctp) would be app/View/Products/index.ctp.
<ul>
<?php foreach($products as $row): ?>
    <li>
        <h1><?php echo $row['Product']['title']; ?></h1>
        <h6>Price: <?php echo $row['Product']['price']; ?></h6>
        <p><?php echo $row['Product']['description']; ?></p>
    </li>
<?php endforeach; ?>
</ul>
Model: Model creation is not required until you need validation or associations. Routes: Open the app/Config/routes.php file and set the default controller and action. Go to the line no.27 and change controller name from "pages" to "products" and action name from "display" to "index". If you want to load the different view for this action, you need to pass the view file name after the action element. Router::connect('/', array('controller' => 'products', 'action' => 'index')); Testing: Run the base URL at the browser, products list would displayed. to be continue...

Comments 1


waiting...sir

Share