{"action":"closed","issue":{"url":"https://api.github.com/repos/slimphp/Slim/issues/1869","repository_url":"https://api.github.com/repos/slimphp/Slim","labels_url":"https://api.github.com/repos/slimphp/Slim/issues/1869/labels{/name}","comments_url":"https://api.github.com/repos/slimphp/Slim/issues/1869/comments","events_url":"https://api.github.com/repos/slimphp/Slim/issues/1869/events","html_url":"https://github.com/slimphp/Slim/issues/1869","id":154015780,"number":1869,"title":"[RFC] Adding Module Support","user":{"login":"geggleto","id":4027602,"avatar_url":"https://avatars.githubusercontent.com/u/4027602?v=3","gravatar_id":"","url":"https://api.github.com/users/geggleto","html_url":"https://github.com/geggleto","followers_url":"https://api.github.com/users/geggleto/followers","following_url":"https://api.github.com/users/geggleto/following{/other_user}","gists_url":"https://api.github.com/users/geggleto/gists{/gist_id}","starred_url":"https://api.github.com/users/geggleto/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/geggleto/subscriptions","organizations_url":"https://api.github.com/users/geggleto/orgs","repos_url":"https://api.github.com/users/geggleto/repos","events_url":"https://api.github.com/users/geggleto/events{/privacy}","received_events_url":"https://api.github.com/users/geggleto/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2016-05-10T14:08:57Z","updated_at":"2016-05-11T17:59:58Z","closed_at":"2016-05-11T17:59:58Z","body":"There was talk on slack this morning about adding some sort of Module support for Slim, allowing our users to share chunks of functionality.\r\n\r\nPlease feel free to comment on the API and the idea itself.\r\n\r\n## SlimModuleManager\r\n\r\n### How to use:\r\n```php\r\nnew SlimModuleManager(Slim\\App $app)\r\n    ->addModule($path_to_config_file);\r\n```\r\n\r\n### Manager Class\r\n\r\n```php\r\nclass SlimModuleManager {\r\n\r\n    protected $container;\r\n\r\n    public function __construct(Slim\\Container $container) {\r\n        $this->container= $container;\r\n    }\r\n\r\n    public function addModule($namespace, Slim\\ModuleConfig $config) {\r\n        $container = $this->container[$namespace];\r\n        $config->__invoke($container);\r\n    }\r\n\r\n    public function addModuleRoutes(Slim\\App $app, Slim\\ModuleConfig $config) {\r\n        $config->regsiterRoutes($app);\r\n    }\r\n\r\n    public function getContainer() {\r\n        return $this->container;\r\n    }\r\n}\r\n\r\n```\r\n\r\n### Module Config\r\n```php\r\ninterface ModuleConfig {\r\n    public function __invoke(Slim\\Container $container);\r\n    public function registerRoutes(Slim\\App $app);\r\n}\r\n```\r\n\r\n### Usage\r\n```php\r\ninclude \"../vendor/autoload.php\";\r\n\r\n$slimModuleManager = new SlimModuleManager(new Slim\\Container([]));\r\n\r\n$authModule = new \\Geggleto\\Auth\\ModuleConfig();\r\n$slimModuleManager->addModule('auth', $authModule);\r\n\r\n$app = new Slim\\App($slimModuleManager->getContainer());\r\n\r\n//If Applicable\r\n$slimModuleManager->addModuleRoutes($app, $authModule);\r\n\r\n```\r\n\r\n## What is a module\r\nA module would be itself a self-contained Slim Application or part of it. A module is a set of objects that get loaded into the Slim Application Container, or a set of actions that add to the Slim Application.\r\n\r\n\r\n## How to install a module\r\n```\r\nphp composer.phar require 'yourslim/module'\r\n```\r\n\r\nYou will then need to setup Composer to autoload these packages. Adding a psr-4 autoload directive for each module you include.\r\n\r\n## Slim Module \r\n\r\n- A module will load it's contents into the Slim Application DIC\r\n- Contains a configuration file which will be executed to \"setup\" the module\r\n\r\n### File Structure\r\n\r\n- config.php <== Module configuration file, you can organize how its structured from there, This should not contain route information\r\n- `src/ | app/ | module/` <== Target the PSR-4 namespace\r\n- routes.php\r\n\r\n"}}