Hi there, I am currently in the process of learning how to buid my own REST API using the Slim v3. I found several tutorials and was able to build multiple routes to send GET and POST requests to my MySQL database. Next up for me is a delete request, but it doesn’t work.
This is my code:
$app->delete('/usuario/[{correo}]', function ($request, $response, $args) {
$sth = $this->db->prepare("DELETE FROM usuarios WHERE email=:correo");
$sth->bindParam("correo", $args['correo']);
$sth->execute();
$todos = $sth->fetchAll();
return $this->response->withJson($todos);
});
I am testing it in Postman and I always have the same problem: 404 Not found. I can not understand it because I think the url is correct (http://localhost:8080/usuario/bbb@bbb.es).
That route works for me. (I didn’t setup a DB to test the database code.) I suspect it is something else in your environment that is causing the 404… Something in your .htaccess, other webserver config, or something within Postman.
App:
<?php
require __DIR__ . '/../vendor/autoload.php';
// Instantiate the app
$settings = require __DIR__ . '/../src/settings.php';
$app = new \Slim\App($settings);
$app->delete('/usuario/[{correo}]', function ($request, $response, $args) {
return $this->response->withJson($args['correo']);
});
// Run app
$app->run();
We seem to know it isn’t working because you are not getting to that route/action. Which webserver are you using? How is it configured? Have you tried the route from curl or another client?