Cherrycake
ExamplesGithub
version 1.x beta
version 1.x beta
  • Introduction
  • Status
  • Changelog
  • Migration
  • Architecture
    • Basics
    • Modules
    • Classes
    • Lifecycle
      • Deep lifecycle
    • Performance
    • Security
    • Patterns
      • Files structure
    • Items
    • Server requirements
  • Guide
    • Getting started
      • Skeleton start
      • Docker start
    • Modules guide
    • Classes guide
    • Actions guide
      • Complex actions
      • Variable path components
      • Accept GET or POST parameters
      • Getting the URL of an action
      • Cached actions
      • Brute force attacks
    • Patterns guide
      • Passing variables to a pattern
      • Nested patterns
      • Cached patterns
    • Cache guide
      • Time To Live
      • Using cache
      • Lists
      • Queues
      • Pools
    • Database guide
      • Basic queries
      • Prepared queries
      • Cached queries
      • Cache key naming
      • Removing queries from cache
    • Items guide
      • Item cache
      • Item lists
      • Items custom filters
      • Items custom ordering
      • Mixing filters and ordering
      • Items with relationships
      • Items cache
    • HtmlDocument guide
    • Css and Javascript guide
      • Modules injecting CSS and JavaScript
    • Session guide
    • Login guide
      • Creating a complete login workflow
    • Locale guide
      • Multilingual texts
      • Domain based site localization
    • Log guide
      • Loading Log events from the database
    • Stats guide
      • Stats events with additional dimensions
      • Loading Stats events from the database
    • Janitor guide
      • Janitor tasks configuration files
    • Command line interface
    • Debugging
  • Reference
    • Core modules
      • Actions
        • Actions methods
      • Browser
      • Cache
        • Cache methods
      • Css
        • Css methods
      • Database
      • Email
      • Errors
      • HtmlDocument
        • HtmlDocument methods
      • ItemAdmin
      • Janitor
        • Janitor methods
      • Javascript
        • Javascript methods
      • Locale
        • Locale methods
      • Log
        • Log methods
      • Login
        • Login methods
      • Output
        • Output methods
      • Patterns
        • Patterns methods
      • Security
        • Security methods
      • Session
        • Session methods
      • Stats
        • Stats methods
      • SystemLog
      • TableAdmin
      • Validate
    • Core classes
      • Action
        • Action methods
        • Action properties
      • AjaxResponseJson
      • BasicObject
        • BasicObject methods
      • CacheProvider
        • CacheProvider methods
      • Color
      • DatabaseProvider
        • DatabaseProvider methods
      • DatabaseResult
        • DatabaseResult methods
        • DatabaseResult properties
      • DatabaseRow
      • Engine
        • Engine methods
        • Engine properties
      • Gradient
      • Item
        • Item methods
        • Item properties
      • Items
        • Items methods
        • Items properties
      • Image
      • JanitorTask
        • JanitorTask methods
        • JanitorTask properties
      • LogEvent
        • LogEvent methods
        • LogEvent Properties
      • LogEvents
        • LogEvents methods
      • Module
        • Module methods
        • Module properties
      • Response
      • Request
        • Request methods
      • RequestParameter
        • RequestParameter methods
      • RequestPathComponent
        • RequestPathComponent methods
      • Result
      • StatsEvent
        • StatsEvent properties
      • StatsEvents
        • StatsEvents methods
      • SystemLogEvent
        • SystemLogEvent methods
        • SystemLogEvent properties
      • SystemLogEvents
        • SystemLogEvents methods
  • Code conventions
  • License
  • Extras
Powered by GitBook
On this page
  • Loading modules
  • Accessing modules
  • Modules lifecycle
  • App module files
  • Modules configuration file
  • Modules constants file
  • Specifying module dependencies

Was this helpful?

  1. Guide

Modules guide

Modules are the fundamental containers of functionality in Cherrycake apps.

PreviousDocker startNextClasses guide

Last updated 4 years ago

Was this helpful?

Modules pack the process-specific logic of an app and have some and over regular classes. Modules come in two flavors:

    • Ready-made modules provided by Cherrycake, they implement all the process-specific functionality behind the Cherrycake architecture.

    • Cherrycake comes with a bunch of aimed to help you build your app. From the main module that routes the requests to your app to modules to work with and files, to control the user , to access and servers, , and much more.

  • App modules

    • Modules created by the developer when creating a new application, which will be in charge of all the processes in your App. You'll have to decide a great App module structure based on the needs of your application.

    • A really simple example of an App module would be the HelloWorld module we created in the section, but a more complex scenario like an e-commerce site might need modules like Products, Cart, Payments, ProductCategories, Search and so, for example.

Loading modules

Modules must be loaded before they can be used, they can be loaded in three ways:

  • As a Base core module, when initializing the engine. Base core modules are loaded right when the engine is initialized. You specify your base modules in the baseCoreModules setup key of . See the section for more details on this.

  • As a dependent module, when they're required by other modules in their or properties. See .

  • At any point in your code, programmatically. Just by calling or .

Accessing modules

Once they're loaded, modules are always accessible as properties of the engine. For example, the module will be available in your code by doing this:

global $e;
$e->Patterns->out("Pattern.html");

Modules lifecycle

When a module is loaded for the first time during a request, this is what happens:

  1. The module file is loaded, and the module instantiated.

  2. The init method of the module is called, which does the following:

    1. If the module has some dependencies on other modules, they're loaded.

    2. If the module has a configuration file, it is loaded.

    3. Any other module-specific initialization is done.

  3. When the engine request has finished, or if any module initialization failed, the end method is called.

App module files

The App modules you create must be stored in the /src directory of your app, and also in their own subdirectory, which has to be named exactly like your module. The file name has to be also the exact name of you module, plus the .php extension.

For example, if you were to create a module called Products, it should be stored on the /src/Products/Products.php directory.

Note that both the subdirectory and the file name itself are case-sensitive.

Modules configuration file

<?php

namespace Cherrycake;

$HtmlDocumentConfig = [
    "title" => "Web page title",
    "description" => "Web page description"
];
class MyModule extends \Cherrycake\Module {
    protected $isConfigFile = true;
    protected $config = [
        "title" => "Default title",
        "description" => "Default description"
    ];
}
$this->getConfig("title");

Modules constants file

Modules can have a constants file specifically aimed to hold their related constant declarations so they will be available anywhere in your code even if the module has not been loaded or initialized. Use them to store constants that are intended to be used outside the module itself.

Constants files are stored in the same directory as the module file, and the file name has to match the exact name of you module, plus the .constants.php extension. For example, the constants file for a module calledProducts would be stored in /src/Products/Products.constants.php, and it might look like this:

<?php

namespace CherrycakeApp;

const PRODUCTS_SIZE_SMALL = 0;
const PRODUCTS_SIZE_MEDIUM = 1;
const PRODUCTS_SIZE_SMALL = 2;

Specifying module dependencies

When your module makes use of another modules regularly, you should specify them as a dependency.

namespace CherrycakeApp\MyModule;

class MyModule extends \Cherrycake\Module {
    protected $dependentCoreModules = [
        "Database",
        "Patterns"
    ];
    
    protected $dependentAppModules = [
        "MyOtherModule"
    ];
}

You can change the default /src directory for the one of your choice by setting the appModulesDir setup key when calling

Modules can have their own configuration file where all settings related to them should be entered. Configuration files are stored under the /config directory by default, but you can set your own directory specifying the configDir setup key in

Module configuration files must have a name that matches the module name, even with upper and lowercase characters. For example, the configuration file for the module must be called /config/Database.config.php

Module configuration files must declare a hash array named in the syntax $<ModuleName>Config. For example, this is how a configuration file for the module would look:

You can set default configuration values that will be used if the configuration file didn't exist, or if a specific configuration key was not set on the configuration file. To do so, set the property of your module, like this:

To get a configuration value from a module, use the method, for example:

See this example working in the site.

For example, the module declares some useful constants in its constants file like DATABASE_FIELD_TYPE_INTEGER, DATABASE_FIELD_TYPE_TIMESTAMP and DATABASE_FIELD_TYPE_STRING.

Set the property of your module to specify which Core modules are required by yours, and the to specify dependencies between your own modules, here's an example:

Database
HtmlDocument
Cherrycake documentation examples
Database
additional benefits
Core modules
Core modules
Actions
Css
JavaScript
Session
Database
Cache
automate tasks
store logs
Deep lifecycle
Patterns
Engine::init
Specifying module dependencies
dependentCoreModules
dependentAppModules
Module::getConfig
Getting started
Engine::init
Engine::init
Engine::loadCoreModule
Engine::loadAppModule
Module::config
Module::dependentCoreModules
Module::dependentAppModules
important differences