I am trying to run the following code in Slim V.4 to delete a record in MariaDB
and practically nothing is happening.
What could it be?
$app->delete('/app/user/delete/{id}', function (Response $response, Request $request, $args){
$id = $args['id'];
$sql = "DELETE FROM users WHERE id = '$id'";
try{
$db = new db();
$db = $db->connect();
$stmt = $db->prepare($sql);
$stmt->execute([$id]);
$db = null;
echo '{"notice": "Usuario borrado con éxito"}';
}catch (PDOException $e) {
echo '{"error": {"text": ' . $e->getMessage() . '}';
}
});
In the insomnia console it says:
INTERNAL SERVER ERROR 500.
And in the console Linux
:
Fatal error: Uncaught TypeError: Argument 1 passed to {closure}() must implement interface Psr\Http\Message\ResponseInterface, instance of Slim\Psr7\Request given, called in /home/martyn/APP/vendor/slim/slim/ Slim/Handlers/Strategies/RequestResponse.php on line 42 and defined in /home/martyn/APP/src/routes/users.php:72
Stacktrace:
#0 /home/martyn/APP/vendor/slim/slim/Slim/Handlers/Strategies/RequestResponse.php(42): {closure}(Object(Slim\Psr7\Request), Object(Slim\Psr7\Response), array)
#1 /home/martyn/APP/vendor/slim/slim/Slim/Routing/Route.php(372): Slim\Handlers\Strategies\RequestResponse->__invoke(Object(Closure), Object(Slim\Psr7\Request) , Object(Slim\Psr7\Response), Array)
#2 /home/martyn/APP/vendor/slim/slim/Slim/MiddlewareDispatcher.php(73): Slim\Routing\Route->handle(Object(Slim\Psr7\Request)) #3 /home/martyn/APP /vendor/slim/slim/Slim/MiddlewareDispatcher.php(73): Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request))
#4 /home/martyn/APP/vendor/slim/slim/Slim/Routing/Route.php(333): Slim\MiddlewareDispatche in /home/martyn/APP/src/routes/users.php on line 72
The anonymous function expects an instance of
Response
and ofRequest
but slim passes them backwards, first he
Request
and then heResponse
We also have that you are passing the id directly in the prepare instead of a marker, the code could look like this:
slim documentation: