CakePHP Tutorial for Beginners guide step by step


CakePHP is an open source web application framework. It follows the Model-View-Controller (MVC) approach and written in PHP. CakePHP makes building web applications simpler, faster and require less code.

This CakePHP tutorial will drive you to the right direction for getting started with CakePHP framework and provide basic guide of CakePHP application development. Our step by step cakePHP tutorial helps beginners for install and configures the CakePHP application. You can learn CakePHP from scratch with our easy tutorial. Also we will develop a sample CakePHP project and it will help you for better understanding the whole process.

Application flow of the CakePHP are given below:

Download CakePHP

At first you need to download the stable release of CakePHP from Github – CakePHP Releases

Basic Configuration

  • Step1: Extract zip file and change folder name with your desire project name. For example cakephp/.
  • Step2: Move the cakephp/ folder to the localhost server. Your directory setup looks like the following.
    • /cakephp
      • /app
      • /lib
      • /plugins
      • /vendors
      • .htaccess
      • index.php
      • README
  • Step3: Create database at the phpMyAdmin. For example cakephp_db.
  • Step4: Open the app/Config/core.php file and make the following changes.
    •  Change the value of Security.salt at Line no.225.
      • Before changes:
      • After changes:
    •  Change the value of Security.cipherSeed at Line no.230.
      • Before changes:
      • After changes:
  • Step5: Rename database.php.default file to database.php at the app/Config/ directory.
  • Step6: Open the “app/Config/database.php” file and make the following changes.
    •  Go to the line no.67 and replace the values of host, login, password, database_name with your database host, database username, database password and database name.
      • Before changes:

  • After changes:

Step7: 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.

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.

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.

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.

Testing: Run the base URL at the browser, products list would displayed.

Leave A Reply

Your email address will not be published.