Slim Framework - DELETE and Framework Function

Asked

Viewed 709 times

0

I’m having problems with slim DELETE, it shows a 404 error, follow the code:

<?php

require '../Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
$app->response()->header('Content-Type', 'application/json;charset=utf-8');

$app->get('/', function () {
echo "SlimProdutos Welcome";
});


$app->delete('/produtos2/:id','deleteProduto');

$app->run();



function getConn()
{
return new PDO('mysql:host=localhost;dbname=Slim',
'root',
'mxk8mxk9',
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
);

}




function deleteProduto($id)
{
$sql = "DELETE FROM produtos WHERE id=:id";
$conn = getConn();
$stmt = $conn->prepare($sql);
$stmt->bindParam("id",$id);
$stmt->execute();
echo "{'message':'Produto apagado'}";
}

I only use http and do not use any other page/form to call it

I’m opening him up like this http://localhost/slimTestes/Slimproducts/products2/111 111 is the id I want to delete!

Another question is about the function of Slim, it basically serves to use the POST, GET, PUT, DELETE, etc...with routes and json, right ? it has other uses ?

  • In your controller you can see the value of $id?

  • how do I do that ?

  • Within deleteProduto() do, echo $id; whether the number is at least displayed.

  • no, gives a 404 slim error "404 Page Not Found"

  • For me I had to change only to DELETE FROM products WHERE id= ?

1 answer

1

Slim is a microframework and yes, it serves for you to map Urls according to GET, POST, DELETE, etc. You don’t necessarily need to return a JSON response, you can return any string you want to show your user, can even use some template engine like - for example - a Twig.

I’ll tell you here the possible problems that may be occurring:

Your . htaccess is working?

The first possible problem is in your . htaccess file (if you’re using apache). If you don’t have a file. htaccess yet, I believe this might work for you (I haven’t tested):

RewriteEngine On
RewriteBase /slimTestes/SlimProdutos/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT]

You are accessing the url through the DELETE method?

You set your route with the method $app->delete so if you access directly from the browser it won’t work.

How to know if that’s the problem?

Change the method to $app->get('/produtos2/:id','deleteProduto') and access through the browser through the link you passed. If you can access the url and your function runs normally, you will know that you were calling the URL through a wrong method (GET or POST).

How to fix if the problem is the method?

When calling your URL via AJAX (or another method you are using), you need to inform that you are using the method DELETE for the requisition.

The easiest way to do this, if using jQuery, is by using the method ajax.

Simple Example:

$.ajax({
    url: '/slimTestes/SlimProdutos/produtos2/111',
    type: 'DELETE',
    success: function(result) {
        // Faça o que quiser com o resultado
        console.log(result);
    }
});

Other tests you can do:

  1. Change your method to: $app->delete('/produtos2/{id}', 'deleteProduto');
  2. Put an anonymous function instead of a string in the second argument

If nothing works out, I advise you to take a look at Documentación Oficial slim framework.

  • No need to configure the Slim .htaccess to rewrite the URL. You can easily refer to the end-point like this: /index.php/delete

Browser other questions tagged

You are not signed in. Login or sign up in order to post.