ndoubismarck/cakephp-modules

Module support plugin for CakePHP

1.0.0 2022-03-30 06:49 UTC

This package is auto-updated.

Last update: 2024-04-29 04:36:25 UTC


README

This CakePHP plugin intends to provide a framework around modules. Modules are a set of reusable classes that can be accessed from anywhere within the application. The idea is to improve maintainability and keep code clean and modular.

Installation

You can install this plugin into your CakePHP application using Composer:

composer require ndoubismarck/cakephp-modules

Load the plugin by adding the following statement in your project's src/Application.php:

public function bootstrap(): void
{
    parent::bootstrap();
    $this->addPlugin('Modules');
}

Baking modules

Bake a module by executing:

bin/cake bake module Example

This will create a module named Example in modules/Example

Loading modules

The ModuleAwareTrait provides functionality for loading module classes as properties of the host object

For example:

  1. In app controller
use Cake\Controller\Controller;
use Modules\ModuleAwareTrait;

/**
 * @property ExampleModule $ExampleModule
 */
class AppController extends Controller
{
    use ModuleAwareTrait;
    
    public function initialize(): void
    {
        $this->loadModule('Example');
    }
}
  1. In any other class
use Modules\ModuleAwareTrait;

/**
 * @property ExampleModule $ExampleModule
 */
class MyClass
{
    use ModuleAwareTrait;
    
    public function __construct()
    {
        $this->loadModule('Example');
    }
}