CakePHP Tutorial for Beginners: What is, Why Use & Features

What is CakePHP?

CakePHP is an open-source framework for the rapid development and maintenance of web applications built in PHP. It is based on the concept of MVC architecture, which helps to build PHP web applications easy and simple with less code. CakePHP also helps you to separate business logic from data layer and presentation layer.

In this CakePHP tutorial for beginners, you will learn CakePHP framework basics like:

Why use CakePHP Framework?

Here, are prime benefits/pros of using CakePHP framework:

History of CakePHP

Now in this CakePHP tutorial, we will cover the history of CakePHP framework:

Features of CakePHP

Here are important features of CakePHP.

CakePHP- Folder Structure

Now in this CakePHP framework tutorial, we will learn about the CakePHP folder structure.

Below is a folder structure of CakePHP:

Folder Description
Tests This folder contains test cases for your application.
Tmp The temp folder stores temporary data.
Vendor This folder helps you to store CakePHP, and other application dependencies will be installed.
Webroot The Webroot stores all the files you want to be publicly reachable.
Bin The bin folder holds the Cake console executables.
Logs It contains your log files, which depends on your log configuration.
Config The config folder store the configuration files CakePHP uses
Plugins This folder contains Plugins for the application. is where the Plugins your application uses are stored.
Src It contains a console task and commands to manage your application.

Locale Stores string files for internationalization.

Model Contains your application's tables, entities, and behaviors.

Configuration of CakePHP

CakePHP comes with a single configuration file by default. It also allows you to modify it according to your requirements. There is a folder called "config" given for this purpose.

Variable Name Description
Debug Allows you to change CakePHP debugging output.
App.namespace Helps you to find app classes under.
App.baseUrl Un-comment this keyword when you don't want to use Apache's mod_rewrite with CakePHP.
App.encoding This command allows you to Define what encoding your application uses.
App.base It is a base directory the app resides in If false, this will be auto-detected.
App.fullBaseUrl Qualified domain name
App.webroot It is a webroot directory.
App.wwwRoot The file path to the webroot.
App.jsBaseUrl Path to the public js directory.
App.paths Configure paths for non-class based resources.
App.cssBaseUrl It is a web path to the public css directory under webroot.
Security.salt It is a random string used in hashing.
Asset.timestamp This variable allows you to appends a timestamp when using proper helpers.

Cake- PHP naming conventions

Cake follows the idea of convention over configuration. Naming convention method allows you to organize the operation of the web application. In this method, more than one word in the name must be separated by "_" when naming the file and camel cases when naming the class.

The MVC parts must follow below given syntax:

File Name Class Name Base Class Name Location
Model File Name Singular form of the table name with .php extension ex: order.php The file name in a Camel case, ex: Order AppModel /app/models
Controller tablename_controller with .php extension, ex: ordcrs_controllcr.php The table name appended Controller, ex: OrdcrsController AppController /app/controllers
View The action name in the controller with .ctp extension, ex: add.ctp No classes; a view c HTML tags and PHP. '
/app/vuews/controller name

Databases Configuration

CakePHP allows you to configure Database in config/app.php file. It provides default connection with provided parameters which you can modify per your choice.

Important parameters are given below:

Database Name of Database
port (optional) encoding The TCP port or Unix socket used to connect to the server.
Timezone Server timezone to set.
Schema It helps you to specify which schema you can use.
unix_socket This Database used by drivers to connect via Unix socket files.
SSL key It is a file path to the SSL key file.
ssl_cert It is a file path to the SSL certificate file.
ssl_ca It is a file path to the SSL certificate authority.
Log This term helps you to perform query logging.
Init A list of queries which should be sent to the database server whenever the connection is created.

Set to true if you want to use reserved words or special characters

How does MVC work in CakePHP?

Now, we will learn how MVC work in CakePHP in this CakePHP framework tutorial.

It is a dynamic way to build the prime mechanism of a web application. The model, the view, and the Controller- separate each other.

MVC architecture in CakePHP
MVC architecture in CakePHP

Cake PHP splits operations into three parts:

Here, are steps to use MVC architecture in CakePHP:

Step 1) The client or user interacts with the view

Step 2) View alerts controller of the specific event.

Step 3) It sends a database request to the Model and Controller updates the model.

Step 4) The model alerts view that it has changed.

Step 5) View receives model data and updates itself according to received data.

Why use MVC?

Disadvantages of using the CakePHP framework

Summary

 

YOU MIGHT LIKE: