Fatal Error if routes are not defined

Tests do not start and and website returns Fatal Error if routes are not defined.

If there is no routes defined, fatal error in the php error logs:

[29-Jul-2021 14:08:26 UTC] PHP Fatal error:  Uncaught Slim\Exception\HttpMethodNotAllowedException: Method not allowed. Must be one of: OPTIONS in /Applications/MAMP/htdocs/flextype/vendor/slim/slim/Slim/Middleware/RoutingMiddleware.php:94
Stack trace:
#0 /Applications/MAMP/htdocs/flextype/vendor/slim/slim/Slim/Middleware/RoutingMiddleware.php(58): Slim\Middleware\RoutingMiddleware->performRouting(Object(Slim\Psr7\Request))
#1 /Applications/MAMP/htdocs/flextype/vendor/slim/slim/Slim/MiddlewareDispatcher.php(147): Slim\Middleware\RoutingMiddleware->process(Object(Slim\Psr7\Request), Object(class@anonymous))
#2 /Applications/MAMP/htdocs/flextype/vendor/slim/slim/Slim/MiddlewareDispatcher.php(81): class@anonymous->handle(Object(Slim\Psr7\Request))
#3 /Applications/MAMP/htdocs/flextype/vendor/slim/slim/Slim/App.php(215): Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request))
#4 /Applications/MAMP/htdocs/flextype/vendor/slim/slim/Slim/App.php(199): Slim\App->handle(Object(Slim\Psr7\Request))
#5 /Applications/MAMP/htdocs/flextype/src/flextype/flextype.p in /Applications/MAMP/htdocs/flextype/vendor/slim/slim/Slim/Middleware/RoutingMiddleware.php on line 94

if I will add any route like this:

app()->get('/hello/{name}', function ($name, Request $request, Response $response) {
    $response->getBody()->write("Hello, $name");

    return $response;

There is no fatal errors on the website,
but when I will run ./vendor/bin/pest
I will get Fatal Error in the console

MacBook-Pro-Sergey:flextype awilum$ ./vendor/bin/pest


  Not found.

  at vendor/slim/slim/Slim/Middleware/RoutingMiddleware.php:91
     87β–•                     ->prepare($routeArguments);
     88β–•                 return $request->withAttribute(RouteContext::ROUTE, $route);
     90β–•             case RoutingResults::NOT_FOUND:
  ➜  91β–•                 throw new HttpNotFoundException($request);
     93β–•             case RoutingResults::METHOD_NOT_ALLOWED:
     94β–•                 $exception = new HttpMethodNotAllowedException($request);
     95β–•                 $exception->setAllowedMethods($routingResults->getAllowedMethods());

      +5 vendor frames 
  6   src/flextype/flextype.php:467

  7   tests/Pest.php:17

I don’t have such issue previously in the Slim3

How to fix this in Slim4?

This looks like a typical CORS preflight request error because there is no corresponding OPTION route for the GET /hello/{name} route.

Read more:

maybe it was my somehow wrong cors setup, but I have fixed it at least website runs and tests running with help of this Application - Slim Framework - by adding this handlers.

Tests runs, but with this message:

MacBook-Pro-Sergey:flextype awilum$ ./vendor/bin/pest
    "statusCode": 404,
    "error": {
        "type": "RESOURCE_NOT_FOUND",
        "description": "Not found."

... and tests results below....

is there some kind of silent mode in the Slim4?

For development purposes, you can enable the error details and error logging as follows:

$app->addErrorMiddleware(true, true, true);

Note: In production, the first parameter should be set to false.

To see all PHP errors and warnings, you could also enable the PHP error reporting (only in development):

// Error reporting
ini_set('display_errors', '1');

The error 404 indicated that there is no matching route for the requested URL.
You may also set the base path so that the router can match the URL from the browser with the path set in the route registration. The basePath defines the URL sub-directory under the document root. This is done with the setBasePath() method.

// http://www.example.com/my-app/users
1 Like