Cake PHP

INTRODUCTION

CAKEPHP is the open source framework for PHP. It makes application development much easier using the MVC based architecture and it is most powerful and easy to grasp. At the time of writing, the current version of CAKEPHP is 3.77.

I will introduce MVC and to how to setup CAKE PHP 3, and an example CRUD Operation.

REQUIREMENTS
  1. XAMP (Local server) with PHP version 5.6 and above.
  2. Composer/GIT
MVC

MVC stand for Model View Control. It means that your application is divided into three parts such as models, views, and controllers. Models are the part that responsible for connecting to database, used to get and save data from the database. Models represent database table in objects that you can use in the controller of your app. They handle data, ranging from Databases to API.

Controllers have the logic of your application. It can get any data saved in your database from the Model objects then modify it or do any functionality then register this manipulated data to objects or variables to be used in views. In the opposite direction, Controller receives data sent by views and manipulate it then call models objects to save it. Views represent the user interface of your application.

Each view is a template file that views the objects and variables registered by a controller, implements HTML, CSS or other presentation languages and has the forms that gather data from users and send it to the controller. Views shouldn’t have any complex logic code, only the simple code that loops through represented data like.

SETUP
  •  Go to root file (Ex: E:\xampp\htdocs) in command line and run the below command to create a new project.

composer create-project –prefer-dist cakephp/app cake3

  • The CAKEPHP 3 will be installed with dependencies in the local with folder name cake3.
  • The landing page should be set up as below in config/routes.php

$routes->connect(‘/’, [‘controller’ => ‘Pages’, ‘action’ => ‘display’, ‘index’]);

  • Create the database ‘cake3’ with users table .

db

  • Go to config/app.php and set up the database credentials.

App.php

app

HELLO WORLD

The index.ctp file to be added in Template/Pages/index.ctp
Navigate to 127.0.0.1/cake3. It will be redirect the page hello world as per the setting in routes.php.

helloWorld

CRUD

Crud: CakePHP Plugin was built to access the databases with ease, and allow developers to have enough flexibility to use it for both rapid prototyping and production applications, even on the same code base saving you time.

  • Crud is very fast to install, a few minutes tops.
  • Crud is very flexible and has tons of configuration options.
  • Usually, the basic code for controller CRUD actions are very simple and always looks the same. Crud will add the actions to your controller so you don’t have to re-implement them over and over again.
  • Crud also provides built in features for JSON and XML API for any action you have enabled through Crud, which eliminates maintaining both a HTML front end and a JSON and/or XML interface for your applications — saving you tons of time and having a leaner code base.
CRUD SETUP
  • Run the below command in command line to install the crud plugin.

composer require friendsofcake/crud

  • Load all the installed plugins by using “Plugin::loadAll();” in config/bootstrap.php
  • Load crud operations in AppController.php

AppController.php

AppController

CRUD OPERATIONS

The below same line can be used for all the crud operations like Index, Add, Edit, View, Delete for user module in UsersController.php

$this->Crud->execute();

CONTROLLERS

UsersController.php

UsersController

MODELS

The basic form validations for users form can be controlled by in Model/Table/UsersTable.php

UsersTable.php

UsersTale

TEMPLATES

Template/Users/index.php

template_index

Template/Users/add.php

template_add

Template/Users/edit.php

template_edit

Template/Users/view.php

template_view

CRUD INDEX

The Index Crud Action paginates over the primary model in the controller.

index

CRUD ADD

The Add Crud Action will update an Add the record if the request is POST or PUT and the data validates – otherwise it will render the same page with validation messages.

add

CRUD EDIT

The Edit Crud Action will update an Existing record if the request is POST or PUT and the data validates – otherwise it will render the same page with validation messages.

edit

 

CRUD VIEW

The View Crud Action will read a record from a data source based on the ID that is part of the request.

view

CRUD DELETE

The Delete Crud Action will delete a record by the id provided in the URL.

CONCLUSION

Based on the above crud operations the cakephp 3 is very easy for developer to install and use. Here we have learnt how to install cakephp 3 with CRUD and use it. We can see some more advanced things like pagination, filtering and scaffolding in CAKEPHP 3 in next part.