Skip to main content

Writing Modules

Extending

Writing Modules

This is how you do it!

Introduction

A module is a part of Morningmedley, or custom, that implements and handles a certain part or feature. Anything can be a module!

Modules are all Services with ServiceProviders.

A Service, in this context, is the module itself and the ServiceProvider is what bootstraps the service and makes it part of the application.

Getting started

To create a module you first need to create your service with all the functionality that you need and afterward set up a provider for it.

Create a directory somewhere meaningful and make sure its contents are autoloaded correctly.

Creating a Module and ServiceProvider

Creating the Module

The module is up to you to create. It can be a simple class or a complex system. That is up to you and your needs.

/src
/Classes
MyModule.php
ServiceProvider.php

Creating the ServiceProvider

The ServiceProvider is a class that should be named ServiceProvider or prefixed with the name of your module.

The ServiceProvider has to important methods: register and boot.

Register

This is where you would add your default config and prepare parts that other modules might need upon booting.

Boot

This is where you boot your module up.

Merging configs

You can have a config file within your module to use as a default. To merge this into the app use the mergeConfigFrom method.

An example

ServiceProvider.php
<?php

namespace MyModule;

class ServiceProvider extends \Illuminate\Support\ServiceProvider
{
public function register()
{
$this->mergeConfigFrom(__DIR__ . "/config/config.php", 'my-module');
}

public function boot(){
$this->make(\MyModule\Classes\MyModule::class)->setup();
}
}

Using Morningmedley modules

Registering Hooks

When registering your service make sure to check if hooks are cached and if not, then register.

ServiceProvider.php
<?php

namespace MyModule;

use MorningMedley\Facades\Hook;

class ServiceProvider extends \Illuminate\Support\ServiceProvider
{
public function boot()
{
if (! Hook::hooksAreCached() ) {
Hook::register('\MyModule\Hooks', __DIR__ . '/Hooks');
}
}
}

Registering Blocks

When registering your service make sure to check if blocks are cached and if not, then register.

ServiceProvider.php
<?php

namespace MyModule;

use MorningMedley\Facades\Block;

class ServiceProvider extends \Illuminate\Support\ServiceProvider
{
public function boot()
{
if (! Block::blocksAreCached() ) {
Block::registerBlocksPath(__DIR__ . '/build/blocks/');
}
}
}

View

ServiceProvider.php
<?php

namespace MyModule;

class ServiceProvider extends \Illuminate\Support\ServiceProvider
{
public function register()
{
\Illuminate\Support\Facades\View::addNamespace('my-module', __DIR__ . '/views');
}
}

Route WIP

ServiceProvider.php
<?php

namespace MyModule;

class ServiceProvider extends \Illuminate\Support\ServiceProvider
{
public function register()
{

}
}