29
Aug

Difference Between Cakephp 2 and Cakephp 3

CakePHP

Cakephp is a free, open-source, web development framework. Programmers can create web applications in structured and without losing flexibility rapidly. It has predefined libraries, functions and tools to get start your coding. It is compatible with PHP version 5.2.8 and greater. It has strict naming conventions. Cakephp follows MVC pattern. MVC is an architectural pattern that describes the structure of our application. MVC divides your application in three parts:

1) MODEL LAYER:

Model layer represents the business logic. In Model Layer we make models which have a direct connection with database tables by using tables name in Model. It helps us to retrieve data from database and convert it into meaningful concepts for your application. Model layer is to handle data like processing, validating and more.

2) VIEW LAYER:

View Layer is a presentational layer that represents data to any supported format. It represents the HTML and documentation data and more. A data that is send by the model will be rendered by View.

3) CONTROLLER LAYER:

Controller handles the request send by user. It contains only your logic part of your application. It render the response from both Model and View Layer, handle the response and show to the user.

DIFFERENCE BETWEEN CAKEPHP 2 AND CAKEPHP 3:

CAKEPHP 2:

The folder structure of cakephp2 is :

We are giving you the overview of those folders and files which is mainly used in projects:

1.1) app

1.1.1)  Config

1.1.1.1) routes.php: In routes.php file you make the structural URL for your application.

1.1.1.2) database.php: All details related with connection established from database written in database.php file.

1.1.1.3) bootstrap.php: When website uploads on browser bootstrap.php file execute firstly. You can make custom functions in this file and execute these functions in any file.

1.1.2)  Console

1.1.3)  Controller: In Controller you make the controller’s action. Your controller should handle the request and call the correct model. Controller is a middle layer between Model and View.

1.1.4)  Lib

1.1.5)  Locale

1.1.6)  Model: In this you make the models of different table names and make connection with tables by gives tables name inside models.

1.1.7)  Plugin: In this folder you include all plugins.

1.1.8)  Test

1.1.9)  tmp: temp folder includes cache, logs, sessions and etc. It maintains the cache, logs and session of whole website.

1.1.10) Vendor: Vendor folder includes third party API files.

1.1.11) View: View folder includes all the view pages which displays on website. All pages extension in cakphp 2 is .ctp

1.1.12) webroot: Webroot folder includes images, css and js files. There is one file in webroot folder is .htaccess, it is used to hide the URL which you don’t want to show in searching on  search engine.

1.1.13) .htaccess

1.2) lib

1.3) vendors

1.4) plugins

1.5) .htaccess

1.6) index.php

1.7) README

2) The ORM was a frankenstein monster that returned a weird array structure. Retrieve data in array format and it decrease the performance of website.

3)  Cakephp 2 includes libraries like this:

Example:

App::uses('AppController', 'Controller');

App::uses('String', 'Utility');

App::uses('Set', 'Utility');

App::uses('HttpSocket', 'Network/Http');

App::import('Vendor', 'Google', array('file' => 'Google/autoload.php'));

4) There is no request object for the session in this version. The use of session to read, write and delete data in cakephp 2 is:

Example:

$this->Session->write('meta','TOM CRUISE');

$this->Session->read('meta');

$this->Session->delete('meta');

5)   Flash Component helps us to set a message and FlashHelper helps us to render a flash message that was set in $_SESSION by FlashComponent.

Example:

// In Controller page

$this->Session->setflash('Message');

//In View page

$this->Session->flash();

6) In cakephp 2 you write database connection part in database.php file which is in Config folder.

7) In cakephp 2 you use Mobile theme in folder app->View->Themed->Mobile.

8) In cakephp 2 you write only model name without using Table word and association uses in model is totally different from Cakephp 3.

Example:

App::uses( "AppModel", "Model");

class Restaurant extends AppModel

{

public $name = "RestaurantMenu";

public $primaryKey = "id";

public $useTable = 'admin_restaurants';

public $hasMany = array(

'Image' => array(

'className' => 'Image',

'foreignKey' => 'place_id',

'dependent' => true

)

);

}

9) In cakephp 2 there is no Cell concept in View.

10)In cakephp 2, there is a concept of bind models. If you bind any association(s) in a model and you have called that model in controller then at the time of executing model that association will also come in an array and you don’t have any need of that association data in that array, at that time you use unbindModel() method to remove that association from that. This will make query longer and complex.

Example:

In Model:

App::uses( "AppModel", "Model");

class Restaurant extends AppModel

{

public $primaryKey = "id";

public $useTable = 'admin_restaurants';

public $hasMany = array(

'Image' => array(

'className' => 'Image',

'foreignKey' => 'place_id',

'dependent' => true

)

);

}

In Controller:

$this-> Restaurant->find(‘all’);

The above query get whole data of Restaurant and Image Model, but you don’t want to get data of Image model. For this you will have to use unbindModel() method to remove Image data from an array.

Example:

$this-> Restaurant-> ->unbindModel(array(‘hasMany’ => array(‘Image’)));

CAKEPHP 3:

The folder structure of cakephp 3 is:

I am giving you the overview of those folders and files which is mainly used in projects:

1.1) bin

1.2) config

1.2.1) app.php: All details related with connection established from database written in app.php file.

1.2.2) bootstrap.php: When website uploads on browser bootstrap.php file execute firstly. You can make custom functions in this file and execute these functions in any file.

1.2.3) routes.php: In routes.php file you make the structural URL for your application.

1.3) logs: Log folder includes log files of whole website.

1.4) plugins: In this folder you includes all plugins and Mobile themes, so that you can make mobile site separately.

1.5) src

1.5.1) Console

1.5.2) Controller: In Controller you make the controller’s action. Your controller should handle the request and call the correct model. Controller is a middle layer between Model and View.

1.5.3) Lib

1.5.4) Model: In this you make the models of different table names and make connection with tables by gives tables name inside models.

1.5.5) Shell

1.5.6) Template: Template folder includes all the view pages which displays on website. All templates files extension in cakphp 3 is .ctp

1.5.7) View: Views are responsible for generating output which is required for the request.

1.6) tests

1.7) tmp: temp folder includes cache, logs, sessions and etc. It maintain the cache, logs and session of whole website.

1.8) vendor: Vendor folder includes third party API files.

1.9) webroot: Webroot folder includes images, css and js files. There is one file in webroot folder is .htaccess, it is used to hide the URL which you don’t want to show in searching on search engine.

1.10) .htaccess

2) The ORM is a real ORM. Retrieve data in object format and you can manage that object according to the conditions and it increase the performance of the website.

3) Cakephp 3 has added namespace feature. Namespace is a collection of libraries. If you want to use libraries features then you have to include those libraries in website.

Example:

namespace app\Controller;

use App\Controller\AppController;

use Cake\Core\Configure;

use Cake\Datasource\ConnectionManager;

use Cake\Event\Event;

use Cake\ORM\TableRegistry;

use Cake\Routing\Router;

4) Cakephp 3 improves the session management. Now we can write, read and delete the session from the request object.

Example:

$this->request->session()->write('meta', 'TOM CRUISE');

$this->request->session()->read('meta');

$this->request->session()->delete('meta');

5) FlashComponent helps us to set a message and FlashHelper helps us to render a flash message that was set in $_SESSION by FlashComponent.

Example:

// In Controller page

$this->Flash->success('Message');

$this->Flash->error('Message');

//In Template page

$this->Flash->render();

6) In cakephp 3 you write database connection part in app.php file which is in config folder.

7) In cakephp 3 Mobile theme includes in plugins folder. So that You can make mobile site separately.

8) In cakephp 3 You write Table word with model name and association uses in model is totally different from Cakephp 2.

Example:

namespace App\Model\Table;

use App\Model\AppModel;

use Cake\Event\Event;

class BarTable extends AppTable

{

public function initialize(array $config){

parent::initialize($config);

$this->dbPrefix('mdp_');

$this->table('bars');

$this->primaryKey('id');

$this->addAssociations([

'hasMany' => [

'BarImage' => ['className' => 'BarImage','foreignKey' => 'bar_id','dependent' => true],

'BarMeta' => ['className' => 'BarMeta','foreignKey' => 'bar_id','dependent' => true]

]

]);

}

}

9) In cakephp 3, there is a concept of Cell in View. These Cells makes in Cell folder which is in View folder. View Cells are mini-controllers. It includes functions and call view logic and render out templates.

You can use these functions in template pages.

In ItemsCell.php file:

Example:

namespace App\View\Cell;

use Cake\View\Cell;

class ItemsCell extends Cell

{

public function status($status,$errorMsg=1,$cancelLink=0,$item=null,$ajaxCancel=null){

// write code here..

}

}

In Template pages:

<?php echo $this->cell("Items::status",[$item['status'],1,1,$item,1]); ?>

10)          In cakephp 3, if you bind any association(s) in model then that association(s) will not call automatically in controller. For calling that association you will have to connect associated Model name with that Model.

Example:

In Model:

namespace App\Model\Table;

use App\Model\AppModel;

use Cake\Event\Event;

class BarTable extends AppTable

{

public function initialize(array $config){

parent::initialize($config);

$this->dbPrefix('mdp_');

$this->table('bars');

$this->primaryKey('id');

$this->addAssociations([

'hasMany' => [

'BarImage' => ['className' => 'BarImage','foreignKey' => 'bar_id','dependent' => true],

'BarMeta' => ['className' => 'BarMeta','foreignKey' => 'bar_id','dependent' => true]

]

]);

}

}

In Controller:

$this->Bar->find(‘all’)->toArray();

If you execute the above query, BarImage and BarMeta associations model will not call automatically, For calling these models you will have to connect associated Model(BarImage, BarMeta) name with Bar Model.

Example:

$this->Bar->find(‘all’)->[‘ BarImage’,’ BarMeta’]->toArray();

This is a correct way to call associated models and this makes query simple and remove complexity.

share

Firebase Realtime Database - Installation and Setup

Firebase Realtime Database - Installation and Setup

previous-blog-arrowPrevious
Foursquare API integration in CakePHP 3

Foursquare API integration in CakePHP 3

next-blog-arrowNext