Not found route

Hello all, I’m rying to follow this tutorial to learn slim.

Everything works fine until i start to use the first post route. Chapter 35:44 - New record endpoint

All other routes work perfectly fine (get routes):

$app->get('/api/users', UserIndex::class);

$app->get('/api/users/{id:[0-9]+}', Users::class . ':show')->add(GetUser::class);

$app->post('/api/users', [Users::class, 'create']);

All classes are in use statements at the top.

This is the error Message I get, when I want to access the /api/users post route:

404 Not Found

The application could not run because of the following error:

Details

Type: Slim\Exception\HttpNotFoundException

Code: 404

Message: Not found.

File: /var/www/html/vendor/slim/slim/Slim/Middleware/RoutingMiddleware.php

Line: 76

Trace

#0 /var/www/html/vendor/slim/slim/Slim/Routing/RouteRunner.php(56): Slim\Middleware\RoutingMiddleware->performRouting(Object(Nyholm\Psr7\ServerRequest)) #1 /var/www/html/vendor/slim/slim/Slim/Middleware/ErrorMiddleware.php(76): Slim\Routing\RouteRunner->handle(Object(Nyholm\Psr7\ServerRequest)) #2 /var/www/html/vendor/slim/slim/Slim/MiddlewareDispatcher.php(121): Slim\Middleware\ErrorMiddleware->process(Object(Nyholm\Psr7\ServerRequest), Object(Slim\Routing\RouteRunner)) #3 /var/www/html/src/App/Middleware/AddJsonResponseHeader.php(13): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(Nyholm\Psr7\ServerRequest)) #4 /var/www/html/vendor/slim/slim/Slim/MiddlewareDispatcher.php(269): App\Middleware\AddJsonResponseHeader->__invoke(Object(Nyholm\Psr7\ServerRequest), Object(Psr\Http\Server\RequestHandlerInterface@anonymous)) #5 /var/www/html/vendor/slim/slim/Slim/MiddlewareDispatcher.php(65): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(Nyholm\Psr7\ServerRequest)) #6 /var/www/html/vendor/slim/slim/Slim/App.php(199): Slim\MiddlewareDispatcher->handle(Object(Nyholm\Psr7\ServerRequest)) #7 /var/www/html/vendor/slim/slim/Slim/App.php(183): Slim\App->handle(Object(Nyholm\Psr7\ServerRequest)) #8 /var/www/html/public/index.php(56): Slim\App->run() #9 {main}

What am I doing wrong?

Thanks for any help

Jevy

Hi @Jevy

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. This is done with the setBasePath() method.

$app->setBasePath('/myapp');

https://www.slimframework.com/docs/v4/start/web-servers.html

Thank you for your answer.

I startet completely fresh with the slim-4-skeleton project.
I had the same issue, until i noticed, i had an additional slash on my /users post route. So it looked like {basepath}/users/

It worked perfectly after deleting the slash.