Modules

GreenCheap uses modules to set application code. Module definition is used to provide boot, routing, and other configuration options. You can listen to events here, you can add custom classes and your own controllers.

Definition: index.php

GreenCheap has a 'ModuleManager' to install and configure a module . At the root of the module index is an ' index.php ' will search the file and wait for it to return a PHP array. Consider this array as a boot for modules code.

By setting the correct properties in this array, Say GreenCheap everything he needs to know about your module.

<?php

/*
 * Returns a php array with a module definition.
 */
return [

    // Necessary: Unique module name
    'name' => 'hello',

];

This minimal example, It is a valid module definition even if it does nothing but be installed by GreenCheap. A module is installed only if the package it contains is enabled in the admin panel.

Note: If you start exploring the inner structure of GreenCheap, a central concept of GreenCheap architecture.

Available Properties

The following table, the module definition array provides an overview of all the keys you can use. The following sections describe the features in detail and contain code samples. Details you can jump directly to each section by clicking on one of its links.

Keys Description More
main Boot code executed when module is loaded
autoload Save namespaces to auto-load
routes Route controllers
permissions Define and save permission names
resources Save resource shortcuts
events Listen to events from GreenCheap or other modules
config Default module configuration
nodes Registering nodes for the Site Tree
node Default options for node configuration
settings Link to a Settings screen
menu Add menu items to the admin panel
widgets Save Widgets
widget Default options for Widget configuration

Boot mode

You can assign a callback function to the main property to run any PHP code. Function takes GreenCheap Application Container Instance as parameter.

use GreenCheap\Application;

// ...

'main' => function (Application $app) {
    // Boot mode
}

This function is called when this module is loaded, so every normal page request. However, the module `index.the php ' file must be in a valid package and the package must be enabled in the admin panel. This means that an extension must be set up and enabled for the boot code to be loaded. If you use this within a theme, only the boot code for the currently enabled theme will execute.

Instead of assigning a callback function and keeping all your code directly in index.php, you can also create a custom module class in a separate file. You then specify the full name of your module class (including the namespace) as the value of the main property.

'main' => 'MyNamespace\\MyModule',

Note For it to work, the referenced namespace must be autoloaded. Make sure that the Mymodule class implements the GreenCheap/Module/ModuleInterface interface. Kısaca bu not için Save custom namespaces you can look at the field.

Save custom namespaces

Forward a list of namespaces and paths to be automatically loaded by GreenCheap. The path is relative to the path of the module, therefore, the following example is "src" of the module package Packages-VENDOR-PACKAGE-Index.php is located in the position Packages/VENDOR/PACKAGE/src, assuming that it is in place.

'autoload' => [

    'GreenCheap\\Hello\\' => 'src'

]

Classes in the linked directory can then be used in use statements in the greencheap codebase

<?php
use GreenCheap\Hello\HelloExtension;

Route controllers

Use the 'route' feature to mount controllers to a route. Learn more about routing and controllers.

'routes' => [

    '/hello' => [
        'name' => '@hello/admin',
        'controller' => [
            'GreenCheap\\Hello\\Controller\\HelloController'
        ]
    ]

]

Define permissions

Define permissions for your module. These can then be assigned to roles in the user and permissions area.

The unique permission names you define (here hello: manage settings) are used as an identifier across the codebase. You can use this identifier to protect your routes from users who do not have this permission and prevent users from performing unauthorized actions.

'permissions' => [

    'hello: manage settings' => [
        'title' => 'Manage settings'
    ]

]

Then a simple way to protect a controller action is to use an annotation as follows. Learn more about annotations.

<?php

class MyController {

    /**
      * @Access("hello: manage settings")
      */
    public function settingsAction() {
        // Control File
    }
}

Save resource shortcuts

You can save prefixes to use as shorter versions when working with ways. For example, packages/vendor/package/views/administrator/settings.php to get started views:admin/settings.use php. GreenCheap saves several ways for extensions and themes by default.

This works every time the GreenCheap file system is used (i.e. when creating a URL for a file path or when creating a view from a controller).

'resources' => [

    'views:' => 'views'

],

Listening to events

Events are triggered at several points in the GreenCheap kernel and potentially by other modules. An event always has a unique name that defines it. You can save callback functions to any activity.

For more information about the event system, see the Events section.

'events' => [

    'view.scripts' => function ($event, $scripts) {
        $scripts->register('hello-settings', 'hello:app/bundle/settings.js', '~extensions');
    }

]

Default module configuration

In most cases, you want to allow the user to change the settings of your module, for example by providing a Settings screen. You can provide a default module configuration to ensure that your module always has some configuration values from the beginning.

'config' => [
    'default' => 'World'
],

Changes made to this configuration array are then stored by the database. The default values are then always combined with the values in the database, and the merge result can be used as the config property of the module object, as you can see in the examples in the next two sections.

Read configuration

To read the config of a module, you can access the config property of the module instance. This object is the result of both the default config stored inside the index.php and changes that are stored in the database.

$config = $app->module('hello')->config;

Write configuration

Use the config() service to store changes to a module configuration. These changes will automatically spread to the database.

// Complete config
$app->config()->set('hello', $config);

// Single Value
$app->config('hello')->set('message', 'Custom message');

Note. if you read the configuration directly from the module, it will still have the old value. After the next request, GreenCheap will combine the changes and make them the config feature of the $module instance.

Registering nodes for the Site Tree

Nodes, It is similar to routes with the main difference that they can be dragged in Site Tree View, and therefore results in a dynamically calculated route.

When you add a node, available in the Site Tree. Click the page Add button to see the drop-down menu for all available node types.

For more information about nodes, see routing.

'nodes' => [

    'hello' => [

        // Node path name
        'name' => '@hello',

        // Label to display in admin panel
        'label' => 'Hello',

        // Controller for this node. Each controller action will be mounted
        'controller' => 'GreenCheap\\Hello\\Controller\\SiteController'
    ]

]

Node options

If your module wants to add a configuration screen to the content editor in the Site Tree, you can use the node property to add default options to the node object (full description for the configuration screen in the theme tutorial)

Theme in the following example, defines a top_style property that is automatically added to each created node object. By default, in this example, the property will have the value uk-block-muted, which is a CSS class we want to create for the 'parent' location.

'node' => [

    'top_style' => 'uk-block-muted'

],

When creating a page in the theme template.php file, you can access this feature from the $params array.

<?php echo $params['top_style'] ?>

node özelliğiyle, her düğüm için varsayılan değeri ayarlarsınız. Kullanıcının bu değerleri değiştirmesine izin vermek için, yönetici alanına bir arayüz eklemek isteyeceksiniz (genellikle bir sayfanın içeriğini düzenlerken Theme sekmesi şeklinde).

To allow the user to change the values of the default widget options that you have defined here, you can add an interface to the admin area. To achieve that, you define a JavaScript component to display the editing screen (Example), register that JavaScript file on the content editor page (Example) and optionally update your Webpack configuration, if you are using Webpack (Example). A complete explanation for this can be found in the Theme tutorial.

Add menu items to the admin panel

You can add menu items to the main navigation of the admin panel. These can be linked to any registered route and restricted to certain access permissions. The Access property determines whether the menu item is visible.

'menu' => [

        // the name can be used for the menu hierarchy
        'hello' => [

            // Label to display
            'label' => 'Hello',

            // Icon to display
            'icon' => 'hello:icon.svg',

            // The URL that this menu item links to
            'url' => '@hello/admin',

            // Optional: Expression to check whether the menu item is active in the current URL
            // 'active' => '@hello*'

            // Optional: Limit access to roles that are assigned special permissions
            // 'access' => 'hello: manage hellos'
        ],

        'hello: panel' => [

            // The top menu item, its 2. so children's menu that allows the level to appear
            'parent' => 'hello',
            'label' => 'Hello',
            'icon' => 'hello:icon.svg',
            'url' => '@hello/admin'
            // 'access' => 'hello: manage hellos'
        ]

    ],

Create a link to a route that displays your settings screen. Set this feature, Allows GreenCheap to create a Settings button next to your theme or extension in the admin panel list.

'settings' => '@hello/admin/settings',

Save widgets

A widget is also a module. With the widgets property you can register all widget module definition files. Each of those files is expected to return a PHP array in the form of a valid module definition. Learn more about widgets.

'widgets' => [

    'widgets/form.php'

],

Widget options

Your module, if it wants to add a configuration screen to the widget editor (this is usually seen when developing a theme), you can use the widget feature to add default options to the widget object (a complete example for the widget configuration screen) In theme Education).

Theme in the following example, defines a` panel ' feature that is automatically added to each created widget object. By default, the property contains an empty string as its value.

'widget' => [

    'panel' => ''

],

When creating widgets, you can access this feature from the $widget->theme sequence.

<?php echo $widget->theme['panel'] ?>

The user has to allow it, to change the values of the default widget options that you define here, you can add an interface to the administrator field. To achieve this, you define a JavaScript component to display the editing screen, you save this JavaScript file to the widget editor page, and if you are using Webpack, you can optionally update your webpack configuration. A full explanation for this can be found in the Theme tutorial.

Follow us on social media

We'll let you know about new announcements, publications and everything else on our social media accounts.

Contact us directly at support@greencheap.net

Greencheap logo

♥ GreenCheap is developed with love and caffeine.