2017 © Pedro Peláez
 

library slim-lazy-controller-connector

Extension for Slim Framework who provides a simple way to connect and 'lazy load' controller and middleware with Slim route.

image

rgsone/slim-lazy-controller-connector

Extension for Slim Framework who provides a simple way to connect and 'lazy load' controller and middleware with Slim route.

  • Sunday, December 4, 2016
  • by rgsone
  • Repository
  • 5 Watchers
  • 8 Stars
  • 205 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 4 Forks
  • 0 Open issues
  • 5 Versions
  • 1 % Grown

The README.md

[DEPRECATED] > Look at new Slim version, (*1)

slim-lazy-controller-connector

Slim LazyControllerConnector is an extension for Slim Framework who provides a simple way to connect and 'lazy load' controllers & middlewares with Slim routes., (*2)

Features

  • Provides a lazy loading for controllers and middlewares
  • The registered controllers are instantiated only once (singleton)
  • Provides a simple way to bind a controller with multiple routes and middlewares

Install

With Composer., (*3)

"require": {
    "slim/slim": "~2.4",
    "rgsone/slim-lazy-controller-connector": "dev-master"
}

Usage

LazyControllerConnector provides two ways to connect Slim routes with controllers. Provides also a simple way to call a method's controller., (*4)

Single route way

Setup, (*5)

<?php

$slim = new \Slim\Slim();
// LazyControllerConnector needs a Slim instance
$connector = new \Rgsone\Slim\LazyControllerConnector( $slim );

Basic usage, (*6)

// connects route '/' with 'MyController' and calls his 'myAction' method via GET method
$connector->connect( 'GET', '/', '\MyController:myAction' );

// anothers examples
$connector->connect( 'GET', '/foo/', '\MyOtherController:myAction' );
// with namespace
$connector->connect( 'GET', '/foo/bar/', '\Foo\Bar\Controller:myAction' );

Also accepts an array of middlewares to call in additional parameters, (*7)

$connector->connect(
    'GET',
    '/middleware/',
    '\MyController:myAction',
    array(
        function() { echo 'middleware'; },
        function() { echo 'another middleware'; },
        // middlewares can also to be called like this
        '\MyMiddleware:myAction'
        // or in any callable forms
    )
);

LazyControllerConnector::connect method returns an \Slim\Route instance, so it is possible to add a route name and/or a route conditions like Slim routing system, (*8)

$connector->connect( 'GET', '/bar/', '\MyController:myAction' )
    ->name( 'my.route.name' );

$connector->connect( 'GET', '/bar/:id', '\MyController:myAction' )
    ->conditions( array( 'id' => '[0-9]+' ) )
    ->name( 'my.route.with.id.name' );

It also possible to bind multiples HTTP methods on a same route, (*9)

// binds with GET and POST methods
$connector->connect( 'GET|POST', '/foo/bar', '\MyController:myAction' );

// binds with GET, POST, DELETE and PUT methods
$connector->connect( 'GET|POST|DELETE|PUT', '/foo/bar', '\MyController:myAction' );

Multiple routes for a same controller

Setup, (*10)

<?php

$slim = new \Slim\Slim();
// LazyControllerConnector needs a Slim instance
$connector = new \Rgsone\Slim\LazyControllerConnector( $slim );

Basic usage, the only required parameter for each route is action, all others are optionnal by default, if the method parameter is not present, the default HTTP method is GET, (*11)

$connector->connectRoutes(

    // controller
    '\MyController',

    // route list
    array(
        '/' => array(
            'action' => 'myAction'
        ),
        '/foo' => array(
            'action' => 'myAction',
            'method' => 'GET'
        ),
        '/bar' => array(
            'action' => 'myAction',
            'method' => 'POST'
        )
    )

);

It is possible to bind multiples HTTP methods for a same route, like LazyControllerConnector::connect each method must be separated by a pipe |, (*12)

$connector->connectRoutes(

    '\Foo\MyController',

    array(
        '/foo/foo' => array(
            'action' => 'myAction',
            // binds GET, POST and PUT methods with this route
            'method' => 'GET|POST|PUT'
        )
    )

);

In addition, it is possible to name route, add conditions and add a middlewares for the same route, (*13)

$connector->connectRoutes(

    '\MyController',

    array(
        '/foo/:id' => array(
            'action' => 'myAction',
            'method' => 'GET|POST',
            // add conditions for :id
            'conditions' => array(
                'id' => '[0-9]+'
            ),
            // names this route
            'name' => 'route.foo.name',
            // middlewares accepts any callable
            'middlewares' => array(
                function() { echo 'route middleware'; },
                // middlewares can also be called like this
                '\Middlewares\MyMiddleware:myAction'
            )
        )
    )

);

As a last, a globals middlewares can be defined, a global middleware is a middleware who is binded with each declared route, (*14)

$connector->connectRoutes(

    '\MyController',

    array(
        '/foo/bar' => array(
            'action' => 'myAction'
        ),
        '/bar/foo' => array(
            'action' => 'myOtherAction'
        )
    ),

    // these middlewares will be called for '/foo/bar' and '/bar/foo' routes
    function() { echo 'global middleware'; },
    '\MyMiddleware:myAction'

);

Full example, (*15)

$connector->connectRoutes(

    '\Foo\Bar\MyController',

    array(
        '/foobar' => array(
            'method' => 'GET',
            'action' => 'myAction',
            'name' => 'route.foobar',
            'middlewares' => array(
                '\Middlewares\MyMiddleware:myAction'
            )
        ),
        '/foobar/:id' => array(
            'method' => 'GET|POST',
            'action' => 'myOtherAction',
            'name' => 'route.foobar.id',
            'conditions' => array( 'id' => '[0-9]+' ),
            'middlewares' => array(
                function() { echo 'single route middleware'; }
            )
        )
    ),

    function() { echo 'global middleware'; },
    '\MyMiddleware:myAction'

);

Calls a method from a controller

Setup, (*16)

<?php

$slim = new \Slim\Slim();
// LazyControllerConnector needs a Slim instance
$connector = new \Rgsone\Slim\LazyControllerConnector( $slim );

Basic usage is to call an action/method from a controller, (*17)

// calls myAction from MyController
$connector->callAction( '\MyController', 'myAction' );
// it also possible to pass args to the called method
$connector->callAction( '\MyController', 'myAction', array( 'my', 'parameters', 11 ) );

LazyControllerConnector::callAction is useful particularly for Slim::notFound method, (*18)

$slim->notFound( function() use ( $connector ) {
    $connector->callAction( '\Controllers\MyNotFoundController', 'do404' );
});

Addition

A \Slim\Slim instance is passed in constructor parameters of each controller, (*19)

<?php

// example of controller
class MyController
{
    private $_slimApp;

    public function __construct( \Slim\Slim $slim )
    {
        $this->_slimApp = $slim;
    }
}

Author

rgsone aka Rudy Marc, (*20)

Thanks

to Josh Lockhart for Slim Framework., (*21)

License

Slim LazyControllerConnector is released under the MIT public license., (*22)

The Versions

04/12 2016

dev-master

9999999-dev http://github.com/rgsone/slim-lazy-controller-connector

Extension for Slim Framework who provides a simple way to connect and 'lazy load' controller and middleware with Slim route.

  Sources   Download

MIT

The Requires

 

framework extension route routing slim microframework controller connector router loading lazy

10/12 2014

v2.2

2.2.0.0 http://github.com/rgsone/slim-lazy-controller-connector

Extension for Slim Framework who provides a simple way to connect and 'lazy load' controller and middleware with Slim route.

  Sources   Download

MIT

The Requires

 

framework extension route routing slim microframework controller connector router loading lazy

04/06 2014

v2.0

2.0.0.0 http://github.com/rgsone/slim-lazy-controller-connector

Extension for Slim Framework who provides a simple way to connect and 'lazy load' controller and middleware with Slim route.

  Sources   Download

MIT

The Requires

 

framework extension route routing slim microframework controller connector router loading lazy

03/06 2014

v1.1

1.1.0.0 http://github.com/rgsone/slim-lazy-controller-connector

Extension for Slim Framework who provides a simple way to connect and 'lazy load' controller(s) with Slim route(s).

  Sources   Download

MIT

The Requires

 

extension routing slim microframework controller connector router lazy loading

06/12 2013

v1.0

1.0.0.0 http://github.com/rgsone/slim-controller-connector

Extension for Slim Framework who provides a simple way to connect a controller with a Slim route.

  Sources   Download

MIT

The Requires

 

extension slim microframework controller router