Quick Reference Study Notes for CakePHP 3.x (Advanced)

Cakephp

CakePHP is an open-source free web framework  for PHP developers. It helps them build complex web applications faster and more efficiently. It is intended to make developing, deploying and maintaining applications much easier.  CakePHP follows the model–view–controller (MVC) approach and modeled after the concepts of Ruby on Rails, and distributed under the MIT License.

1. MVC in Cakephp:-

MVC stands for model view controller. MVC is not a design pattern, it is an architectural pattern that describes a way to structure our application and explains responsibilities and interactions of each part in that structure:

  1. The Model represents the application data It should be responsible for managing almost everything regarding your data, its validity, and its interactions.

  2. The View renders a presentation of model data. Views are responsible for generating the specific output required for the request.

  3. The Controller handles and routes requests made by the client. CakePHP’s controller isn't designed to be able to pass data to a non-view file (like .php). CakePHP's views should be ending with .ctp.


 

From the above diagram, you can understand how your cakephp application will works:

 

a) First the request will be sent from Client to Dispatcher.

b) The Dispatcher selects the appropriate controller & action based on routing rules

c) The Controller interacts with the required Models.

d) The Model usually represent database tables. It fetches the required data from database by firing mysql queries.

e) Then controller gets the data from model and hands it to a view. The view takes this data and gets it ready for presentation to the client.

f )The view uses Helpers and Cells to generate the response body and headers. It sends the response back to the client.
 

2. Installation:

Installing CakePHP is simple and easy. You can install it from composer or you can download it from github − https://github.com/cakephp/cakephp/releases.

Two ways of installation:

Composer-  Composer is a tool for managing project dependencies. You can create a CakePHP project using Composer by running below commands on terminal.

php composer.phar create-project --prefer-dist cakephp/project_name

Download From Github- After downloading it from github, extract all the files in a folder called “CakePHP” which is existed in “/var/www/html/”. You can give custom name to folder but we have used “CakePHP”.


3. Features of Cakephp:

  • MVC Architecture

  • View Helpers for AJAX, JavaScript, HTML Forms and More

  • Inbuilt validation

  • ACL Functionality and Security

  • Easy CRUD (Create, Read, Update, Delete) Database Interactions.

  • Easily extendable with plug-ins

  • Quick and flexible

  • Code generation

  • Flexible Caching

  • Localization


4. Folder Structure:

 


bin:   The bin folder holds the Cake console executables.

      For example- bake the user's controller by executing following commands:

                  cd /path/to/cakephp-3-2-6/bin/

cake bake controller Users

config:  The (few) configuration files which is used by your cakephp application are located in this folder. Database connection details, bootstrapping, core configuration files and more should be stored here.

 

logs:  Configuring Log should be done during your application's bootstrap phase. The config/app.php file is intended for just this. This folder contains your log files based on log configuration.

plugins: It stores all the plugins which are used by your application.

src: This folder is important part of your application for development purpose. It is where your application’s files will be placed. It contains various sub-folders which are listed as below:

 

  • Console: It contains the console commands and console tasks for your application.

  • Controller: It lists your controller and component files..

  • Locale: It stores string files for internationalization. Internationalization refers to the ability of an application to be localized.

  • Model: It contains your application’s tables, entities and behaviors.

  • View: It contains cells, helpers, and template files. In these files, presentation classes are located.

  • Template: Elements, error pages, layouts, and view template files are placed in this folder.

tests: In which folder, you will put the test cases of your application.

tmp: It stores the temporary data of your application. There are three sub folders:

  • cache: It will contain the model-cache and the location of classes/objects and parsed 'locale' files.

  • sessions: It will contain the session information.

  • tests:  It will contains the information regarding the test cases and test fixtures.

 

vendor:  In which folder,  CakePHP and other application dependencies are installed. You should never edit any files located in this folder, otherwise your application will not work further.

webroot: The webroot directory is the public document root of your application. It contains all the files you want to be publicly reachable.

5. Component:

Developers can write the common code or logic in a component class and access it in each controller by loading that component class. It helps you to reuse the code and make your controllers tiny and clean.

Below are the list some commonly used CakePHP components

  • Authentication

  • Cookie

  • Cross Site Request Forgery

  • Flash

  • Security

  • Pagination

  • Request Handling



 

6. Routing:

Routing is a feature that maps URLs to controller actions.  By using this feature, you can create more readable and flexible URLs. You can define routes for your application in config/routes.php file. There are two methods for  defining routes:

Example of scoped route builder:  

Router::scope('/', function ($routes) {
  $routes->connect('/login', ['controller' => 'Users', 'action' => login]);
});
Example of Static method:  

Router::connect('/login', ['controller' => Users, 'action' => login]);


Both the methods will execute the “login” function of  “UsersController”. By typing following url in browser, you can invoke the both type of routing methods:

http://localhost/<project-name>/login


*NOTE : "This study material is collected from multiple sources to make a quick refresh course available to students."



 

This website uses cookies to improve user experience. By using our website you consent to all cookies in accordance with our Cookie Policy. More info. I Agree