I'll preface this with saying that I'm a crappy programmer, I'm sure that what I want to do could be done in 10 lines of node or Rails or something else, but PHP is what I have available.
So, I'm hoping to find a simple PHP library which wraps the database calls in an API that looks similar to the RESTful model.
I've had little success trying to find such a thing -- searching for PHP CRUD or PHP REST turns up several zillion pages, and I've no idea how to filter through them.
I'm really trying to keep things simple here, I don't want a big framework like Zend or something. The models I'm dealing with in Backbone are really simple. I just want to send GET s to, say, /notes/3 or POST s to /notes , etc, and have PHP do the right thing to a database.
Perhaps I'm asking too much, but it seems to me that this is what other frameworks like Rails provide. Any suggestions? TIA...
Source: Tips4all
Codeigniter, to me, is the easiest of the Rails-like frameworks. It's bare bones, and you can build a CRUD app from scratch easily.
ReplyDeleteThe biggest issue with rolling your own app is security. Codeigniter can help you build a less hackable site by shielding you from many of the common security risks, such as using $_POST arrays directly, and not properly filtering your data. Not to mention the many helper classes it offers such as form validation.
You can view the documentation on their website. It's very easy to use as long as you remember the navigation is hidden at the top of each page. :D
Do you understand how CRUD works internally? From a PHP standpoint, it could be as easy as having a switch statement over each REST call possibility.
ReplyDeleteSee this page here:
http://www.codethinked.com/building-epic-win-with-backbone-js
Skip to the section titled "Wiring It Up To The Server".
Your PHP script simply has to satisfy those requirements.
A simple prototype code:
switch($_SERVER['REQUEST_METHOD']){
case 'POST':
// create new item
break;
case 'GET':
// get item(s)
break;
case 'PUT':
// update item
break;
case 'DELETE':
// delete item
break;
}
You will also need to set up a .htaccess file as follows (to handle accessing non-existent urls):
# Turn on rewrite engine and redirect broken requests to index
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L,QSA]
</IfModule>
A URL like http://mysite.com/1 doesn't real exist, which is why you need to route.
There are lots of restfull frameworks for PHP, have a look here and here.
ReplyDeleteI personally like fat-free-framework but you need PHP 5.3 for it.
Also there is a lot of support for Tonic and Recess seems quite interesting.
Also all the standard frameworks have some sort of rest support (zend, code igniter, symfony and the likes)
You should find your fit ..
Also if you already have your mysql queries ready, you could convert the mysql results directly into json like this :
function recordSetToJson($mysql_result) {
$rs = array();
while($rs[] = mysql_fetch_assoc($mysql_result)) {
// you don´t really need to do anything here.
}
return json_encode($rs);
}
After that it's quite easy to associate with urls ..
From : Convert MySQL record set to JSON string in PHP
You can use silex https://github.com/fabpot/Silex a simple framework based on symphony 2. With Silex you can easily route and map action.
ReplyDeleteYou have access to the basic CRUD element and you can call a function with the URL component.
There are some examples on the documentation :
http://silex-project.org/doc/usage.html
new REST api solution
ReplyDeleteexamples
http://www.apifysnippets.com/
code
https://github.com/apify
tutorial
http://blog.fedecarg.com/2011/09/11/building-a-restful-web-api-with-php-and-apify/
UPDATE:
another rest solution for PHP:
http://luracast.com/products/restler/
:)
you might want to look at Slim:
ReplyDeletehttp://www.slimframework.com/
its definitely light-weight and can give you what it seems like you're looking for, an easily deployed RESTful backend with php.