2017 © Pedro Peláez
 

library slim-controller

Slim Framework controller creator

image

juliangut/slim-controller

Slim Framework controller creator

  • Saturday, August 5, 2017
  • by juliangut
  • Repository
  • 2 Watchers
  • 24 Stars
  • 6,188 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 9 Forks
  • 0 Open issues
  • 11 Versions
  • 20 % Grown

The README.md

PHP version Latest Version License, (*1)

Build status Style Code Quality Code Coverage Total Downloads, (*2)

Slim3 controller management

NOTICE: To anyone using this package you should consider moving to juliangut/slim-routing

Class route creation boilerplate, allows you to define your controller classes as services to be pulled out from container easily, extending from a base controller class., (*3)

I strongly suggest you don't use this library but for rapid prototyping or certain use cases. If you really want to follow SOLID principles, don't use this package and either directly inject your dependencies in the controller class, use the container to create and inject dependencies or try another container such as PHP-DI with juliangut/slim-php-di, (*4)

I cannot stress this enough, this library is meant to be used with default Slim container and for certain cases only., (*5)

Installation

Best way to install is using Composer:, (*6)

composer require juliangut/slim-controller

Then require_once the autoload file:, (*7)

require_once './vendor/autoload.php';

Usage

use \Jgut\Slim\Controller\Resolver;

// Define your controllers
$controllers = [
    'MyController',
];

// Create Slim app and fetch DI Container
$app = new \Slim\App();
$container = $app->getContainer();

// Register Controllers
foreach (Resolver::resolve($controllers) as $controller => $callback) {
    $container[$controller] = $callback;
}

// Define route (\MyController has already been registered)
$app->get('hello/app', '\MyController:dispatch');

// Run app
$app->run();

If your controller implements Jgut\Slim\Controller\Controller it has the DI container automatically injected, you can access it by getContainer method., (*8)

If your controller extends Jgut\Slim\Controller\Base you can also directly access container services the same way you do on a Closure route callback. Simply take care to not define class attributes with the same name as services in the container. To do this container is injected in the controller and __get and __isset magic methods are defined to look into the container., (*9)

use Jgut\Slim\Controller\Base as BaseController;

class MyController extends BaseController
{
    public function displatch($request, $response, array $args)
    {
        // Pull Twig view service given it was defined
        return $this->view->render($response, 'dispatch.twig');
    }
}

You can use the resolver to define your class routes callback and not implement Controller or extend Base on those classes, in this case your controller won't have access to the container but it will still be a valid callback., (*10)

Important notice

As a general rule of thumb directly injecting container is considered a bad practice as you are actually hiding your dependencies, by fetching them from the container, instead of defining them in the class. You'll be using the container as a service locator rather than a true DIC., (*11)

Caveat

This controller registration works only for controllers whose constructor doesn't need any parameters. In case you need a controller with paramenters in its __construct() method you can still benefit from \Jgut\Slim\Controller\Controller but you have to register it yourself., (*12)

use Jgut\Slim\Controller\Controller;

$container['\MyController'] = function($container) {
    $controller = new \MyController('customParameter');

    // Set container into the controller
    if ($controller instanceof Controller) {
        $controller->setContainer($container);
    }

    return $controller;
}

Contributing

Found a bug or have a feature request? Please open a new issue. Have a look at existing issues before, (*13)

See file CONTRIBUTING.md, (*14)

License

Release under BSD-3-Clause License.

See file LICENSE included with the source code for a copy of the license terms, (*15)

The Versions

05/08 2017

dev-master

9999999-dev http://github.com/juliangut/slim-controller

Slim Framework controller creator

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

framework slim controller

04/09 2016

1.5

1.5.0.0 http://github.com/juliangut/slim-controller

Slim Framework controller creator

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

framework slim controller

09/11 2015

0.3

0.3.0.0 http://github.com/juliangut/slim-controller

Slim Framework controller creator

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

framework slim controller

11/09 2015

1.4

1.4.0.0 http://github.com/juliangut/slim-controller

Slim Framework controller creator

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

framework slim controller

06/09 2015

1.3.1

1.3.1.0 http://github.com/juliangut/slim-controller

Slim Framework controller creator

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

framework slim controller

17/08 2015

1.3

1.3.0.0 http://github.com/juliangut/slim-controller

Slim Framework controller creator

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

framework slim controller

16/08 2015

1.2

1.2.0.0 http://github.com/juliangut/slim-controller

Slim Framework controller creator

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

framework slim controller

15/08 2015

1.1

1.1.0.0 http://github.com/juliangut/slim-controller

Slim Framework controller creator

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

framework slim controller

13/08 2015

1.0

1.0.0.0 http://github.com/juliangut/slim-controller

Slim Framework controller creator

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

framework slim controller

15/03 2015

0.2

0.2.0.0 http://github.com/juliangut/slim-controller

Slim Framework controller creator

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

framework slim controller

15/03 2015

0.1

0.1.0.0 http://github.com/juliangut/slim-controller

Slim Framework controller creator

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

framework slim controller