dev-master
9999999-dev http://github.com/martynbiz/slim3-moduleA simple class for loading modules in a Slim 3 project
MIT
The Requires
- php >=5.3.0
- slim/slim ~3.0
The Development Requires
mvc slim modules slim3
A simple class for loading modules in a Slim 3 project
Modules for Slim3, (*1)
Composer, (*2)
"require-dev": { "martynbiz/slim3-module": "dev-master" }
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; }); } }
It is possible for moodules to use libraries of other modules. To allow this, module initializer will load modules in the following order:, (*8)
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; }); } }
A simple class for loading modules in a Slim 3 project
MIT
mvc slim modules slim3