I'm building an API using Slim Framework and I wrote a script that creates the routes and add It's middlewares dinamically. The problem is that for some reason the routes are being applied to every route in the app. How can I apply the middleware on one route only?
This is the route.cfg file
#[HTTP Verb] [Route] [Controller::method] [middleware|middleware]
GET /usuario/autenticar UsuarioController:autenticar log
GET /usuario/listar[/{id}] UsuarioController:listar log|autenticar
GET /usuario/encerrarSessao UsuarioController:encerrarSessao log|autenticar
POST /usuario/cadastrar UsuarioController:cadastrar log|autenticar
PUT /usuario/editar UsuarioController:editar log|autenticar
DELETE /usuario/deletar UsuarioController:deletar log|autenticar
This is the script that read the routes file
<?php
use Slim\App;
use Slim\Http\Request;
use Slim\Http\Response;
return function (App $app) {
$container = $app->getContainer();
$routesFile = file(__DIR__ . '/routes.cfg');
foreach ($routesFile as $fileLine) {
$fileLine = str_replace("\n", "", $fileLine);
$fileLine = preg_replace('/\s+/', ' ', $fileLine);
$args = explode(' ', $fileLine);
if (strpos($fileLine, '#') !== false || count($args) < 3) continue;
$verb = array_key_exists(0, $args) ? $args[0] : null;
$endpoint = array_key_exists(1, $args) ? $args[1] : null;
$controller = array_key_exists(2, $args) ? $args[2] : null;
$routeMiddleware = array_key_exists(3, $args) ? $args[3] : null;
$app->{$verb}($endpoint, "$controller");
if (isset($routeMiddleware) && strlen($routeMiddleware) > 0) {
$routeMiddleware = trim($routeMiddleware);
$middlewares = explode('|', $routeMiddleware);
foreach ($middlewares as $middlewareFunction) {
$app->add(function($request, $response, $next) use ($middlewareFunction) {
return Middleware::{$middlewareFunction}($request, $response, $next);
});
}
}
}
};
And this is my Middleware class
<?php
use Slim\App;
class Middleware {
public static function autenticar($request, $response, $next) {
//Do stuff...
return $next($request, $response);
}
public static function log($request, $response, $next) {
//Do stuff...
return $next($request, $response);
}
}
In this case, the problem is that this line registers the middleware for all routes:
$app->add(function($request, $response, $next) use ($middlewareFunction) {
Registering a middleware for a single route works like this:
$this->get('/', \App\Action\HomeIndexAction::class)
->add(MyMiddleware::class);
I would try to change your code like this:
<?php
use Slim\App;
use Slim\Http\Request;
use Slim\Http\Response;
return function (App $app) {
$routesFile = file(__DIR__ . '/routes.cfg');
foreach ($routesFile as $fileLine) {
$fileLine = str_replace("\n", '', $fileLine);
$fileLine = preg_replace('/\s+/', ' ', $fileLine);
$args = explode(' ', $fileLine);
if (strpos($fileLine, '#') !== false || count($args) < 3) {
continue;
}
$verb = $args[0] ?? null;
$endpoint = $args[1] ?? null;
$controller = $args[2] ?? null;
$routeMiddleware = $args[3] ?? null;
// This line has changed
$route = $app->{$verb}($endpoint, $controller);
if (isset($routeMiddleware) && $routeMiddleware !== '') {
$routeMiddleware = trim($routeMiddleware);
$middlewares = explode('|', $routeMiddleware);
foreach ($middlewares as $middlewareFunction) {
// This line has changed
$route->add(function ($request, $response, $next) use ($middlewareFunction) {
return Middleware::{$middlewareFunction}($request, $response, $next);
});
}
}
}
};