Click to See Complete Forum and Search --> : PHP PHP REST(ful) API


ultddave
July 5th, 2011, 07:25 AM
Hey,

I'm trying to create a PHP framework that uses the MVC design pattern and REST (http://en.wikipedia.org/wiki/Representational_State_Transfer).

I know REST uses the HTTP GET, POST, PUT and DELETE to indicate what action should be taken against the given URL.

The PHP code to check the method/action in my ArticleController:

$requestMethod = $_SERVER['REQUEST_METHOD'];
switch($requestMethod)
{
case 'GET':
$this->show($articleid);
break;
case 'PUT':
$this->edit($articleid);
break;
case 'POST':
$this->create($articleid);
break;
case 'DELETE':
$this->delete($articleid);
break;
}


But I'm having trouble understanding how you can send PUT and DELETE requests via <a> tags and html forms?

For example:

<form method="post" action="/Article/2">
<input type="text" name="content" maxlength="20" size="20" />
<input type="submit" value="Submit" />
</form>


Ofcourse the action="Article/2" will be dynamic in the final result of the webpage.

But the <form> only supports method "post" (and "get"), but I want to update (HTTP PUT) the resource. How do I do that?

The same question for delete:


<a href="/Article/2"> Delete this article </a>


How can i send an HTTP DELETE to that URI instead of a GET? Do I need to use Ajax to send an XMLHttpRequest with a 'PUT' to that URL?

Thanks in advance.

PeejAvery
July 5th, 2011, 03:13 PM
An <a> tag would only every allow you to sent GET requests. The only way to simulate PUT and DELETE is to write a custom GET or POST to the processing PHP script.

ultddave
July 5th, 2011, 03:41 PM
Ok, thanks for the info. I'll try it that way. ;)