2017 © Pedro PelĂĄez
 

library slimpower-slim

Dynamically instantiated controller classes for Slim Framework

image

matiasnamendola/slimpower-slim

Dynamically instantiated controller classes for Slim Framework

  • Monday, June 19, 2017
  • by matiasamendola
  • Repository
  • 2 Watchers
  • 0 Stars
  • 34 Installations
  • PHP
  • 6 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

SlimPower - Slim

Latest version ![Total Downloads][ico-downloads], (*1)

Latest Stable Version Latest Unstable Version Total Downloads Monthly Downloads Daily Downloads composer.lock available, (*2)

An extension to [Slim Framework][1] that allows you use to dynamically instantiated controllers with action methods wherever you would use a closure or callback when routing., (*3)

The controller can optionally be loaded from Slim's DI container, allowing you to inject dependencies as required., (*4)

Additionally, this extension implements Json Middleware & View with great ease., (*5)

Installation

Look at Installation File, (*6)

Usage - Dynamic controller instantiation

Use the string format {controller class name}:{action method name} wherever you would usually use a closure:, (*7)

e.g., (*8)

require 'vendor/autoload.php';

$app = new \SlimPower\Slim\Slim();

$app->get('/hello:name', 'App\IndexController:home');

You can also register the controller with Slim's DI container:, (*9)

<?php

require 'vendor/autoload.php';

$app = new \SlimPower\Slim\Slim();

$app->container->singleton('App\IndexController', function ($container) {
    // Retrieve any required dependencies from the container and
    // inject into the constructor of the controller.

    return new \App\IndexController();
});

$app->get('/', 'App\IndexController:index');

Example controller

SlimPower - Slim Controller will call the controller's setApp(), setRequest() and setResponse() methods if they exist and populate appropriately. It will then call the controller's `init()`` method., (*10)

Hence, a typical controller may look like:, (*11)

<?php

namespace App;

class IndexController {
    // Optional properties.
    protected $app;
    protected $request;
    protected $response;

    public function index() {
        echo "This is the home page";
    }

    public function hello($name) {
        echo "Hello, $name";
    }

    // Optional setters.
    public function setApp($app) {
        $this->app = $app;
    }

    public function setRequest($request) {
        $this->request = $request;
    }

    public function setResponse($response) {
        $this->response = $response;
    }

    // Init
    public function init() {
        // Do things now that app, request and response are set.
    }
}

Usage - Json Middleware

To include the middleware and view you just have to load them using the default Slim way. Read more about Slim Here (https://github.com/codeguy/Slim#getting-started), (*12)

require 'vendor/autoload.php';

$app = new \SlimPower\Slim\Slim();

$app->view(new \SlimPower\Slim\Middleware\Json\JsonView());
$app->add(new \SlimPower\Slim\Middleware\Json\JsonMiddleware());

Example method

All your requests will be returning a JSON output. the usage will be $app->render( (int)$HTTP_CODE, (array)$DATA);, (*13)

Example code


$app->get('/', function() use ($app) { $app->render(200, array( 'msg' => 'welcome to my API!', )); });

Example output

{
    "msg":"welcome to my API!",
    "error":false,
    "status":200
}

Errors

To display an error just set the error => true in your data array. All requests will have an error param that defaults to false., (*14)

$app->get('/user/:id', function($id) use ($app) {

    // Your code here.

    $app->render(404, array(
        'error' => TRUE,
        'msg'   => 'user not found',
    ));
});

```json { "msg":"user not found", "error":true, "status":404 }, (*15)


You can optionally throw exceptions, the middleware will catch all exceptions and display error messages. ```php $app->get('/user/:id', function($id) use ($app) { // Your code here. if(...) { throw new \Exception("Something wrong with your request!"); } });
{
    "error": true,
    "msg": "ERROR: Something wrong with your request!",
    "status": 500
}

Embedding response data and metadata in separate containers

It is possible to separate response metadata and business information in separate containers., (*16)

To make it possible just init JsonView with containers names

require 'vendor/autoload.php';

$app = new \SlimPower\Slim\Slim();

$app->view(new \SlimPower\Slim\Middleware\Json\JsonView("resource", "meta"));
$app->add(new \SlimPower\Slim\Middleware\Json\JsonMiddleware());

Response

{
    "resource":{
        "msg":"welcome to my API!"
    },
    "meta":{
        "error":false,
        "status":200
    }
}

Routing specific requests to the API

If your site is using regular HTML responses and you just want to expose an API point on a specific route, you can use Slim router middlewares to define this., (*17)

function jsonResponse(){
    $app = \SlimPower\Slim\Slim::getInstance();
    $app->view(new \SlimPower\Slim\Middleware\Json\JsonView());
    $app->add(new \SlimPower\Slim\Middleware\Json\JsonMiddleware());
}

$app->get('/home',function() use($app){
    // Regular HTML response.
    $app->render("template.tpl");
});

$app->get('/api','jsonResponse',function() use($app){
    // This request will have full JSON responses.

    $app->render(200, array(
        'msg' => 'welcome to my API!',
    ));
});

Middleware

The middleware will set some static routes for default requests. if you dont want to use it, you can copy its content code into your bootstrap file., (*18)

IMPORTANT: remember to use $app->config('debug', false); or errors will still be printed in HTML, (*19)

Credits

License

The MIT License (MIT). Please see License File for more information., (*20)

Example project

Look at slimpower-slim-example., (*21)

The Versions

19/06 2017

dev-master

9999999-dev https://github.com/matiasnamendola/slimpower-slim

Dynamically instantiated controller classes for Slim Framework

  Sources   Download

MIT

The Requires

 

api json slim controller

06/12 2016

v0.0.1-alpha

0.0.1.0-alpha https://github.com/matiasnamendola/slimpower-slim-controller

Dynamically instantiated controller classes for Slim Framework

  Sources   Download

MIT

The Requires

 

api middleware json config slim controller view