Tools for Slim Framework

1.0.0 2019-05-30 02:19 UTC


Tools for Slim Framework 3.


composer require renanonline8/slim-utils



This class has the purpose of creating a controller class

  1. Create the class
namespace App\Controller;

use \SlimUtils\Controller\BaseController;
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

final class Controller extends BaseController
    public function control(Request $request, Response $response, Array $args)
        //implement the code...

        //for use a container...
  1. Create a container with Controller
$container['ControllerIndex'] = function($c) {
    return new \App\Controller\ControllerIndex($c);
  1. Implement the route
$app->get('/[{name}]', 'Controller:control');


This class has the purpose be a Middleware abstract with easy access of container

  1. Create the class
namespace App\Middleware;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use \SlimUtils\Middleware\BaseMiddleware;
use \SlimUtils\Middleware\InterfaceMiddleware;

class ExampleMid extends BaseMiddleware implements InterfaceMiddleware
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
        //for use a container...
        $response = $next($request, $response);

        return $response;
  1. Add Middleware at the route
$app->get('/[{name}]', 'Controller:control')->add(new ExampleMid($container));