- Modules 101
- File Structure
- Creating a new module
- Module Front-End
- Module Back-End
- Using Assets
- Loading modules
- Module parameters
- Module functions
- Checking if the module is in Live edit mode
- Things to know
- Modules
- Setting up the Album module
- Autoloading
- Configuration
- Informing the application about our new module
- Donate
Modules 101
Microweber modules are PHP scripts that can be placed on the website. They are executed in a sandboxed environment, but can access Microweber, Laravel and any custom dependency’s functionality. Modules help developers make easy modifications and add functionality to user pages.
Microweber comes bundled with a handful of preinstalled modules that you can examine and extend. We encourage learning and the principles of creating modules by looking at the existing code.
In most cases, modules are «blocks» that users drag and drop wherever they need them on their website. Galleries, menus, displaying a post, listing posts in given category, contact forms, etc. are examples of such modules.
Modules don’t have to depend on the website’s content or design and can have their own presentation layer.
File Structure
Each module is contained within its own folder in userfiles/modules . In order for new modules to be available, you have to open the modules administration page in the back-end and click Reload Modules .
Every module needs the following files to work
Filename | Description |
---|---|
config.php | this file contains the info for your module |
index.php | this file loads the module after it is dropped or opened from the frontend |
admin.php | when you open the module settings from the admin or from the live edit, this file is loaded |
functions.php | optional file, it is loaded on system start with the website |
icon.png | icon for your module (size 32×32) |
Creating a new module
There are just few steps you need to make in order to create new module
- Make new folder for your module for example userfiles/modules/example_module/
- Make index.php and admin.php files, so your module can be displayed
- Create a config.php file with the information for your module
Module Front-End
The index.php file in the module’s folder is being rendered anywhere on the site the user has placed the module.
If you drop the module on a page you will be able to see your text.
Module Back-End
Users can access administration pages for their installed modules from both the front-end (in a modal) or from the back-end. Creating an administration page for your module has many advantages. It’s handy for a number of cases, such as allowing users to edit global or instance-specific module settings.
The admin.php file in your module’s root directory is executed and rendered when the user accesses the administration of your module.
Using Assets
Upon execution modules are injected with a $config array inside their scope. It contains the module path, name and other module info.
The module’s filesystem location and URL can be found in the $config array. You can use it to load assets or other scripts from the module folder.
The $config[‘url_to_module’] key contains the full url to the module folder (ex. http://.com/userfiles/modules/example_module/ )
The $config[‘path’] key contains the full path to the module folder (ex. /var/www/userfiles/modules/example_module/ )
Provided there is a file named logo.png in your module folder you can easily display it with code like this:
Loading modules
In Microweber the modules are loaded via the tag inside your template, where the «type» parameter is the path to your module. Additionally they can be dragged and dropped by the user in the editable regions of your site.
This code will load the file userfiles/modules/example_module/index.php
If you wish to load a file other than index.php you can do it by
This code will load the file userfiles/modules/example_module/something.php
Module parameters
The modules can accept parameters, which are passed as html attributes.
You can access the parameters inside the module from the $params array. Open index.php in your module folder and add this code
Each module have an «id» which is accessible with $params[‘id’] and its unique for every module, unless you set it as html attribute. If you do not put an «id» attribute on your module, Microwber will generate one.
Module functions
If you create a file named functions.php in the module folder, this file will be loaded automatically on every request. You can use it to make your custom functions, autoload classes and do whatever you like.
Checking if the module is in Live edit mode
Things to know
- You can load other modules in your modules
- You can have editable regions in your modules
- Important: in order you module to show, you need to click realod modules button in admin panel
Modules
laminas-mvc uses a module system to organise your main application-specific code within each module. The Application module provided by the skeleton is used to provide bootstrapping, error, and routing configuration to the whole application. It is usually used to provide application level controllers for the home page of an application, but we are not going to use the default one provided in this tutorial as we want our album list to be the home page, which will live in our own module.
We are going to put all our code into the Album module which will contain our controllers, models, forms and views, along with configuration. We’ll also tweak the Application module as required.
Let’s start with the directories required.
Setting up the Album module
Start by creating a directory called Album under module with the following subdirectories to hold the module’s files:
laminas-mvc-tutorial/ /module /Album /config /src /Controller /Form /Model /view /album /album
The Album module has separate directories for the different types of files we will have. The PHP files that contain classes within the Album namespace live in the src/ directory. The view directory also has a sub-folder called album for our module’s view scripts.
In order to load and configure a module, Laminas provides a ModuleManager . This will look for a Module class in the specified module namespace (i.e., Album ); in the case of our new module, that means the class Album\Module , which will be found in module/Album/src/Module.php .
Let’s create that file now, with the following contents:
namespace Album; use Laminas\ModuleManager\Feature\ConfigProviderInterface; class Module implements ConfigProviderInterface < public function getConfig() < return include __DIR__ . '/../config/module.config.php'; >>
The ModuleManager will call getConfig() automatically for us.
Autoloading
While Laminas provides autoloading capabilities via its laminas-loader component, we recommend using Composer’s autoloading capabilities. As such, we need to inform Composer of our new namespace, and where its files live.
Open composer.json in your project root, and look for the autoload section; it should look like the following by default:
We’ll now add our new module to the list, so it now reads:
Once you’ve made that change, run the following to ensure Composer updates its autoloading rules:
Configuration
Having registered the autoloader, let’s have a quick look at the getConfig() method in Album\Module . This method loads the config/module.config.php file under the module’s root directory.
Create a file called module.config.php under laminas-mvc-tutorial/module/Album/config/ :
namespace Album; use Laminas\ServiceManager\Factory\InvokableFactory; return [ 'controllers' => [ 'factories' => [ Controller\AlbumController::class => InvokableFactory::class, ], ], 'view_manager' => [ 'template_path_stack' => [ 'album' => __DIR__ . '/../view', ], ], ];
The config information is passed to the relevant components by the ServiceManager . We need two initial sections: controllers and view_manager . The controllers section provides a list of all the controllers provided by the module. We will need one controller, AlbumController ; we’ll reference it by its fully qualified class name, and use the laminas-servicemanager InvokableFactory to create instances of it.
Within the view_manager section, we add our view directory to the TemplatePathStack configuration. This will allow it to find the view scripts for the Album module that are stored in our view/ directory.
Informing the application about our new module
We now need to tell the ModuleManager that this new module exists. This is done in the application’s config/modules.config.php file which is provided by the skeleton application. Update this file so that the array it returns contains the Album module as well, so the file now looks like this:
(Changes required are highlighted using comments; original comments from the file are omitted for brevity.)
return [ 'Laminas\Form', 'Laminas\Db', 'Laminas\Router', 'Laminas\Validator', 'Application', 'Album', //
As you can see, we have added our Album module into the list of modules after the Application module.
We have now set up the module ready for putting our custom code into it.
Donate
Support Laminas Developers Directly