Hi guys,
I just started to use SlimFramework.
I followed instructions from here Installation - Slim Framework and I’m trying to handle custom 404 (Not Found) and 405 (Method not Allowed) errors but maybe I’m missing something as it is not behaving as I’m expecting.
Here are some details:
PHP Version: 7.3
Slim Version: 4
Web Server: internal PHP server (php -S localhost:8888 -t public public/index.php)
index.php:
<?php use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; use Slim\Factory\AppFactory; require __DIR__ . '/../vendor/autoload.php'; $app = AppFactory::create(); $app->get('/test', function (Request $request, Response $response, $args) { $response->getBody()->write("Test route"); return $response; }); $app->post('/', function (Request $request, Response $response, $args) { $response->getBody()->write("Hello from main route"); return $response; }); $errorMiddleware = $app->addErrorMiddleware(false, true, false); $errorMiddleware->setErrorHandler( HttpNotFoundException::class, function (ServerRequestInterface $request, Throwable $exception, bool $displayErrorDetails) { $response = new Response(); $response->getBody()->write('Custom 404 handler'); return $response->withStatus(404); }); $errorMiddleware->setErrorHandler( HttpMethodNotAllowedException::class, function (ServerRequestInterface $request, Throwable $exception, bool $displayErrorDetails) { $response = new Response(); $response->getBody()->write('Custom 405 Handler'); return $response->withStatus(405); }); $app->run();
When I run internal PHP Web Server and navigate to http://localhost:8888 I get standard SlimFramework 405 page:
Slim Application Error
The application could not run because of the following error:
Details
Type: Slim\Exception\HttpMethodNotAllowedException
Code: 405
Message: Method not allowed.
File: C:\Projects\Slim\Login\vendor\slim\slim\Slim\Middleware\RoutingMiddleware.php
Line: 94
Trace
#0 C:\Projects\Slim\Login\vendor\slim\slim\Slim\Routing\RouteRunner.php(60): Slim\Middleware\RoutingMiddleware->performRouting(Object(Slim\Psr7\Request)) #1 C:\Projects\Slim\Login\vendor\slim\slim\Slim\Middleware\ErrorMiddleware.php(89): Slim\Routing\RouteRunner->handle(Object(Slim\Psr7\Request)) #2 C:\Projects\Slim\Login\vendor\slim\slim\Slim\MiddlewareDispatcher.php(123): Slim\Middleware\ErrorMiddleware->process(Object(Slim\Psr7\Request), Object(Slim\Routing\RouteRunner)) #3 C:\Projects\Slim\Login\vendor\slim\slim\Slim\MiddlewareDispatcher.php(64): class@anonymous->handle(Object(Slim\Psr7\Request)) #4 C:\Projects\Slim\Login\vendor\slim\slim\Slim\App.php(174): Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request)) #5 C:\Projects\Slim\Login\vendor\slim\slim\Slim\App.php(158): Slim\App->handle(Object(Slim\Psr7\Request)) #6 C:\Projects\Slim\Login\public\index.php(40): Slim\App->run() #7 {main}
Seems that Custom error handler is not fired.
The same happens when I try to access route that is not configured.
When accessing http://localhost:8888/route I get:
Slim Application Error
The application could not run because of the following error:
Details
Type: Slim\Exception\HttpNotFoundException
Code: 404
Message: Not found.
File: C:\Projects\Slim\Login\vendor\slim\slim\Slim\Middleware\RoutingMiddleware.php
Line: 91
Trace
#0 C:\Projects\Slim\Login\vendor\slim\slim\Slim\Routing\RouteRunner.php(60): Slim\Middleware\RoutingMiddleware->performRouting(Object(Slim\Psr7\Request)) #1 C:\Projects\Slim\Login\vendor\slim\slim\Slim\Middleware\ErrorMiddleware.php(89): Slim\Routing\RouteRunner->handle(Object(Slim\Psr7\Request)) #2 C:\Projects\Slim\Login\vendor\slim\slim\Slim\MiddlewareDispatcher.php(123): Slim\Middleware\ErrorMiddleware->process(Object(Slim\Psr7\Request), Object(Slim\Routing\RouteRunner)) #3 C:\Projects\Slim\Login\vendor\slim\slim\Slim\MiddlewareDispatcher.php(64): class@anonymous->handle(Object(Slim\Psr7\Request)) #4 C:\Projects\Slim\Login\vendor\slim\slim\Slim\App.php(174): Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request)) #5 C:\Projects\Slim\Login\vendor\slim\slim\Slim\App.php(158): Slim\App->handle(Object(Slim\Psr7\Request)) #6 C:\Projects\Slim\Login\public\index.php(40): Slim\App->run() #7 {main}
Can you please let me know what I’m missing