Slim Framework - DELETE and Framework Function

0

I'm having problems with the slim DELETE, it has 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 I do not use any other page / form to call it

I'm opening it up like this link 111 is the id that I want to delete!

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

    
asked by anonymous 18.03.2016 / 18:53

1 answer

1

Slim is a microframework and yes, it is for you to map URLs according to the GET, POST, DELETE, etc. methods. You do not necessarily need to return a response in JSON, you can return any string you want to show to your user, you can even use some template engine like - for example - the Twig .

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

Is your .htaccess working?

The first possible problem is in your .htaccess file (if you are using apache). If you do not have a .htaccess file yet, I think this may work for you (not tested):

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

Are you accessing the url via the DELETE method?

You have defined your route with the method $app->delete so if you go directly through the browser it will not work.

How do I know if this is 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 its function is executed normally, you will know that you were calling the URL through a wrong mette (GET or POST).

How to correct if the problem is the method?

When you call your URL through AJAX (or another method you are using), you need to inform that you are using the DELETE method for the request.

The easiest way to do this, if you're using jQuery, is to use the ajax method.

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:

  • Change your method to: $app->delete('/produtos2/{id}', 'deleteProduto');
  • Place an anonymous function instead of a string in the second argument
  • If this is not resolved, I advise you to read the Official Documentation of the Slim Framework.

        
    25.05.2016 / 14:05