Modular architecture using CakePHP 3

I, author of medical healthcare IoT application, would like to share my real life experience with modular CakePHP 3. Modular architecture makes the system easier to understand, design and manage complex interdependent systems. Modularity fits an environment where several programmers share work.

Modular development of a system

Modules can be optimized independently of other modules, so that failure of one module does not cause other modules to fail.

GarazLab : Cheapest $1 marketplace for code script and themes. Buy Sell innovative web solutions simply & easily in lowest affordable price from our marketplace. Buy production ready scripts and themes with full after sale professional service warranty from our marketplace.

MVC is completely different from modular structure. In a non-modular approach, it is very difficult to find the interface between sections of code.

Non-modular approach

A module should have below characteristics:

  1. Independent: It will be simple structure and independent. it allows to proceed separately on each item.
  2. Reusable: It should be reusable with other modules or components. It can be freely combined with each other or produce new systems, possibly in an environment quite different from the one in which they were initially developed.
  3. Understandable: Each module should be human readable. One should understand the objective of the module very easily from it’s code.
  4. Scalability: It should be easily scalable. We can easily change the necessary features of the module.
  5. Protection: If something goes wrong in a module, it should not effect others.

With CakePHP 3.0, we have the basic MVC structure. Using an MVC framework forces you to divide and organize your code according to the framework conventions. Presentation code goes to the view, data manipulation code goes to the model, and the request manipulation logic goes to the controller. Here is the default structure of CakePHP 3.0:

CakePHP 3.0 basic folder structure
CakePHP 3.0 basic folder structure (continued)

How to implement modular structure with CakePHP 3.0?

Modular programming is the breaking down of a website into parts that are of more manageable size and have a well-defined purpose. Larger, complicated websites are constructed by using modular programming approach.

  1. The first step is to break the task into its basic parts.
  2. Those parts will lead to defining intermediate steps.
  3. For each part, create a comprehensive and efficient solution.
  4. Each part will be validated individually by unit test.

If we follow the above principles with CakePHP 3.0, then we have several areas to consider. Let’s discuss about it step by step:

Config :

In the configuration folder, we will set separate folders by ‘environment’. like below:

‘development’ ‘production’ ‘staging’

Environment wise configuration folder

Each environment will contain the specific configuration files. For each specific operations like session, db, error etc, we should have separate configuration files. To create separate folders/files on configuration, add the below code on bootstrap.php file.

try {
Configure::config('default', new PhpConfig());
Configure::load('app', 'default', false);
if (env('ENVIRONMENT') == 'production') {
//production server config
} else if (env('ENVIRONMENT') == 'staging') {
//staging server config
} else {
//development server config
Configure::load('development/db', 'default');
Configure::load('development/cache', 'default');
Configure::load('development/email', 'default');
Configure::load('error', 'default');
Configure::load('session', 'default');
} catch (\Exception $e) {
exit($e->getMessage() . "\n");


If we write all the routes on routes.php, it becomes very long, hard to maintain, not readable. So, you can include necessary modular route files under /config/route/ folder.

require CORE_PATH . 'config' . DS . 'route' . DS . 'auth' . DS . 'login.php';

In this way, it would be more easily manageable.


In the controller folder, we organize our controllers into relevant folder group. You need to add proper namespace into each controller.

Modular structure for controller

Always try to separate common reusable functions into relevant ‘Component’. You can create modular structure for ‘Component’ also. But as components can be used by multiple controller across module, so, I prefer not to create module for components.


Similar as controllers, we should create modular structure for ‘Model’ folder also. There might be tables, but we will group them into relevant folders with proper namespace. We created modules on ‘Table’ & ‘Entity’ folder.

Modular structure for Model


Please create a necessary view files on the ‘Template’ folder by module also. First, you need to have single/multiple layout files for each different layout. associate appropriate layout file with the corresponding view file.

Modular structure for View


If you want to implement test cases for each unit test, then it is also better to create module on those also.

Webroot (CSS / JS / IMG):

For each asset files, we can create modular pattern also. it is not mandatory, but it is encouraged. but I am highly propose to maintain a minimum modular structure on ‘img’ folder.

This is the basic building block for a modular approach with CakePHP 3.0. If you have a better suggestion, please let me know in the below comments. Happy coding… cheers…

Leave a Reply