CakePHP MVC Architecture

cakephp mvc architecture

CakePHP MVC Architecture

Cakephp is an architecture framework, which uses the MVC (Model View Controller) design pattern.It is open source,fast development framework for PHP.
Model view controller (MVC) is an architectural pattern used in web development, software development. In this architecture –
Model section is used to write database related functions.
View section is used to write design template parts.
Controller section is used to write business logic.

In cakephp the routing can be set in routes.php (app/Config/routes.php). You can set multiple routes in this file. For example

Set your home page

Router::connect('/', array('controller' =>; 'controller_name', 'action' =>; 'action_name','layout'=>;'layout_name'));

 

For any page like about us

Router::connect('/about-us', array('controller' => 'pages', 'action' => 'aboutUs'));

For admin section

Router::connect('/admin', array('controller' => 'controller_name', 'action' => 'action_name', 'admin' => true,'layout'=>'layout_name','prefix' => 'set your prefix'));

The prefix key is used to set prefix before each controller url after base url.

CakePHP MVC Architecture

Database connection can be set in database.php(app/Config/database.php). You can see there are two database connection arrays. Basically the idea behind this is to allow developers to switch between databases for example the production database and staging database

 

public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'HOST_NAME',
'login' => 'USERNAME',
'password' => 'PASWORD',
'database' => 'main_databse_name',
'prefix' => '',
//'encoding' => 'utf8',
);

public $test = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'HOST_NAME',
'login' => 'USERNAME',
'password' => 'PASSWORD',
'database' => 'test_database_name',
'prefix' => '',
//'encoding' => 'utf8',
);

 

Security salt and cipher seed in CakePHP

When you install the Cakephp then on first time run it asked about to change the security salt and security cipherseed. Actually Cakephp uses the security salt for hashing methods and security cipher seed for the encrypt and decrypt string.
You can change these options from core.php(app/Config/core.php).

/**
* A random string used in security hashing methods.
*/
Configure::write('Security.salt', 'DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9miA');

/**
* A random numeric string (digits only) used to encrypt/decrypt strings.
*/
Configure::write('Security.cipherSeed', '76859309657453542496749683645A');

How CakePHP works

When we are all set to run our cakephp application then on very first it calls bootstrap.php It can be change through htaccess or index.php file

Scaffolding in CakePHP

Scaffolding is a technique that allows a developer to define and create a basic application CRUD operations that can create, retrieve, update and delete objects. Scaffolding in CakePHP also allows developers to define how objects are related to each other, and to create and break those links.

To add scaffolding to your cakephp application, in the controller, add the $scaffold variable:

<?php
class ArticlesController extends AppController
{
  var $scaffold;
}
?>

Note : If you are using scaffolding in your application then donot create any function or method in your controller

Components in CakePHP

Components are nothing but the common logics that are shared between controllers. They are very useful when a common logic is required between different controllers. And here comes the reusability property of OOPS concept.

CakePHP also comes with a fantastic set of core components you can use to aid in:

Security
Sessions
Access control lists
Emails
Cookies
Authentication
Request handling

Creating component in cakephp

Create the file in /app/controllers/components/math.php. The basic structure for the component would look something like this:

<?php
class YourComponentNameComponent extends Object
{
    function addSomeData($data1, $data2)
    {
         return $data1 + $data2;
    }
}
?>

Use component in your controller

<?php
class ArticlesController extends AppController
{
    var $components = array('YourComponentName');
}
?>

Helper in CakePHP

Helpers in CakePHP are classes like component used in presentation layers of your cakephp application. Mainly, The helpers in cakephp contain presentational logics that are share between many views, elements, or layouts of application.

Creating Helper – To create helpers you need add a class file in /app/view/helpers. For example Anchor.php

<?php
class AnchorHelper extends AppHelper
{
    function editHyperLinks($title, $url)
   {
        // Here the logic to modify the anchor of view
   }
}
?>

User Helper

<?php
class ArticlesControllerController extends Controller
{
    var $helpers = array('Form', 'Html', 'Anchor');
}
?>

If you want use you Helper globally in your cakephp application then add it to your AppController.php file

<?php
class AppController extends Controller
{
    var $helpers = array('Form', 'Html', 'Anchor');
}
?>

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.