What is a module?
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.
Each module is contained within its own folder in
In order for new modules to be available, you have to open the modules administration page in the back-end and click
Every module needs the following files to work
|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 32x32)|
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
admin.phpfiles, so your module can be displayed
- Create a
config.phpfile with the information for your module
<?php $config = array(); $config['name'] = "My Awesome Module"; $config['link'] = "https://github.com/"; $config['description'] = "The best module ever!"; $config['author'] = "Serous Sam"; $config['ui'] = true; //you can drop this module in live edit $config['ui_admin'] = true; //your module is visible in the admin $config['position'] = "98"; $config['version'] = "0.01";
index.php file in the module's folder is being rendered anywhere on the site the user has placed the module.
<h1>Hello from my module</h1>
If you drop the module on a page you will be able to see your text.
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.
admin.php file in your module's root directory is executed and rendered when the user accesses the administration of your module.
<h1>Admin part my module</h1>
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
You can use it to load assets or other scripts from the module folder.
<link rel="stylesheet" href="<?php print $config['url_to_module']; ?>style.css" /> <script src="<?php print $config['url_to_module']; ?>script.js"></script>
$config['url_to_module'] key contains the full url to the module folder (ex.
$config['path'] key contains the full path to the module folder (ex.
Provided there is a file named
logo.png in your module folder you can easily display it with code like this:
<img src="<?php echo $config['url_to_module']; ?>logo.png" />
In Microweber the modules are loaded via the
<module type="your_module_name" /> 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.
<module type="example_module" />
This code will load the file
If you wish to load a file other than index.php you can do it by
<module type="example_module/something" />
This code will load the file
The modules can accept parameters, which are passed as html attributes.
Here is example of it:
<module type="example_module" my-param="foo" other-parameter="bar" />
You can access the parameters inside the module from the
$params array. Open index.php in your module folder and add this code
<?php $id = $params['id']; $foo = $params['my-param']; $bar = $params['other-parameter']; print("Module id: ". $id); print("My parameter is: ". $foo); print("My other parmeter is: ". $bar); ?>
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.
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
<?php if(in_live_edit()): ?> <?php print notif("Click here to edit this module"); ?> <?php endif; ?>
Things to know
- You can load other modules in your modules
- You can have editable regions in your modules