2017 © Pedro Peláez
 

library slim3-module

A simple class for loading modules in a Slim 3 project

image

martynbiz/slim3-module

A simple class for loading modules in a Slim 3 project

  • Thursday, June 9, 2016
  • by martynbiz
  • Repository
  • 4 Watchers
  • 6 Stars
  • 142 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 1 Versions
  • 7 % Grown

The README.md

Slim v3 module

Introduction

Modules for Slim3, (*1)

Installation

Composer, (*2)

"require-dev": {
    "martynbiz/slim3-module": "dev-master"
}

Getting started

This library expects a directory within the modules directory, and within that module a {module_name}/Module class:, (*3)

modules/
├── Hello
│   └── Module.php

public/index.php, (*4)

$moduleInitializer = new \MartynBiz\Slim3Module\Initializer($app, [
    'autoload' => [ // <--- list of modules to autoload
        'Hello',
    ],
    'modules_path' => '/path/to/modules',
]);

$moduleInitializer->initModules();

composer.json, (*5)

TODO add path to namespace e.g. App/Module, (*6)

/path/to/modules/Hello/Module.php, (*7)

namespace Hello;

use Slim\App;
use MartynBiz\Slim3Module\AbstractModule;

class Module extends AbstractModule
{
    public function initRoutes(App $app)
    {
        $app->get('/hello/{name}', function ($request, $response) {
            $name = $request->getAttribute('name');
            $response->getBody()->write("Hello, $name");

            return $response;
        });
    }
}

Advanced usage

It is possible for moodules to use libraries of other modules. To allow this, module initializer will load modules in the following order:, (*8)

  1. Initiate class maps for all modules
  2. Import settings from all modules
  3. Initiate dependencies for all modules
  4. Initiate app middleware for all modules
  5. Lastly, initiate routes for all modules

To configure modules, simple override the methods of the AbstractModule class:, (*9)

namespace Hello;

use Composer\Autoload\ClassLoader;
use Slim\App;
use Slim\Container;
use MartynBiz\Slim3Module\AbstractModule;

class Module extends AbstractModule
{
    public function getModuleConfig()
    {
        return [
            'logger' => [
                //...
            ],
        ];
    }

    public function initClassLoader(ClassLoader $classLoader)
    {
        $classLoader->setPsr4("Hello\\", __DIR__ . "/src");
    }

    public function initDependencies(Container $container)
    {
        $container['logger'] = function ($c) {
            $settings = $c->get('settings')['logger'];
            $logger = new \Monolog\Logger($settings['name']);
            $logger->pushProcessor(new \Monolog\Processor\UidProcessor());
            $logger->pushHandler(new \Monolog\Handler\StreamHandler($settings['path'], \Monolog\Logger::DEBUG));
            return $logger;
        };

        //...
    }

    public function initMiddleware(App $app)
    {
        //...
    }

    public function initRoutes(App $app)
    {
        $app->get('/hello/{name}', function ($request, $response) {
            $name = $request->getAttribute('name');
            $response->getBody()->write("Hello, $name");

            return $response;
        });
    }
}

The Versions

09/06 2016

dev-master

9999999-dev http://github.com/martynbiz/slim3-module

A simple class for loading modules in a Slim 3 project

  Sources   Download

MIT

The Requires

 

The Development Requires

mvc slim modules slim3