slimphp / Slim

Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.
http://slimframework.com
MIT License
11.98k stars 1.95k forks source link

[RFC] Adding Module Support #1869

Closed geggleto closed 8 years ago

geggleto commented 8 years ago

There was talk on slack this morning about adding some sort of Module support for Slim, allowing our users to share chunks of functionality.

Please feel free to comment on the API and the idea itself.

SlimModuleManager

How to use:

new SlimModuleManager(Slim\App $app)
    ->addModule($path_to_config_file);

Manager Class

class SlimModuleManager {

    protected $container;

    public function __construct(Slim\Container $container) {
        $this->container= $container;
    }

    public function addModule($namespace, Slim\ModuleConfig $config) {
        $container = $this->container[$namespace];
        $config->__invoke($container);
    }

    public function addModuleRoutes(Slim\App $app, Slim\ModuleConfig $config) {
        $config->regsiterRoutes($app);
    }

    public function getContainer() {
        return $this->container;
    }
}

Module Config

interface ModuleConfig {
    public function __invoke(Slim\Container $container);
    public function registerRoutes(Slim\App $app);
}

Usage

include "../vendor/autoload.php";

$slimModuleManager = new SlimModuleManager(new Slim\Container([]));

$authModule = new \Geggleto\Auth\ModuleConfig();
$slimModuleManager->addModule('auth', $authModule);

$app = new Slim\App($slimModuleManager->getContainer());

//If Applicable
$slimModuleManager->addModuleRoutes($app, $authModule);

What is a module

A 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.

How to install a module

php composer.phar require 'yourslim/module'

You will then need to setup Composer to autoload these packages. Adding a psr-4 autoload directive for each module you include.

Slim Module

geggleto commented 8 years ago

Alternative Approach

Multiple Slim Apps, one installation

https://github.com/geggleto/hydra

KuroThing commented 8 years ago

What is the scope of a Module? Your example

//If Applicable
$slimModuleManager->addModuleRoutes($app, $authModule);

implies that a Module would handle the rendering of pages as well? How would that work?

If you want your site to be rendered using PHP-View, but your Auth Module rendered pages using Twig-View, that seems kind of silly and ludicrous.

There is also the problem of consistency in the styling of the page, since Slim is a framework and doesn't have an existing CSS class selection to abide by, Modules could implement anything, and require CSS which conflicts with your actual sites CSS.

While it sounds like a nice idea, imo it seems kind of out of scope of what a Micro Framework should offer? At Least for your example.