20

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 GETs to, say, /notes/3 or POSTs 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...

6 Answers6

15

EDIT Nov 2018: Although I wouldn't knock CodeIgniter, nowadays Laravel (currently 5.5) is the framework I use.

Here is a good article that sums up the reasons I use Laravel.

To get jump started, I recommend Laracasts. It's a subscription video tutorial service that goes in depth on how to use Laravel (and other web dev related things).

ORIGINAL ANSWER:

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.

The 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

John Sanders
  • 405
  • 2
  • 5
  • 2
    +1 It's more secure and better structured than going from scratch, but it's less restrictive than Symfony, CakePHP, Yii, etc. If my project is basic 'one-page-one-content' type stuff with a few simple forms then I use CI. If it's more advanced, I use Symfony or Yii. The big downsides of CI are it's poor caching, and the way it makes it hard to reuse actions across controllers. – Dan Blows May 11 '11 at 11:02
  • 1
    After some consideration I think Code Igniter is pretty close to what I need. For one thing there are a ton of docs out there (http://codeigniter.com/wiki/Tutorials/), and there is also what looks to be a pretty good REST plugin: https://github.com/philsturgeon/codeigniter-restserver . Thanks everyone for posting. –  May 12 '11 at 20:10
13

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.

See 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.

Edit: In case you are planning to use PUT or DELETE in HTML forms, forget it. As of writing this, it has not been accepted in HTML5, and pretty much all browsers fail to support this. My "fix" to this is to use GET for GET requests, and POST for all the rest (POST itself, PUT and DELETE). Example:

<form action="POST" action="/users/5">
    <input type="hidden" name="method" value="DELETE"/>
    <button>Delete User #5</button>
</form>

This, however, is not a problem with AJAX since apparently you can set XMLHttpRequest Method to anything you want without problems.

Christian
  • 27,509
  • 17
  • 111
  • 155
  • 1
    Thanks for the suggestions, it's helpful but not quite what I'm looking for. The back-end example on the post you link is in ASP.net, not PHP as I requested. Also, I understand the basic idea of a RESTful service: each HTTP verb needs to be mapped to code that modifies the database. But what I'm seeing here suggests to me that I have to go and write (say) individual PDO statements, SQL, etc, for each HTTP verb. Aren't there any libraries out there that offer an API to this sort of thing in PHP? –  May 08 '11 at 05:42
  • @pat - Regarding the post I linked to, that was to demonstrate what the verbs did, not to serve as example code. If you exected my answer to be in the form of "the perfect solution is XYZ", spare it. I'm showing you how it works so you can do it yourself. It's very easy to make it work given my above example code. There is simply no need for any frameworks to get this thing running. – Christian May 08 '11 at 08:02
  • 1
    I'm sorry you seem to be offended by my reply, Christian. Thanks for your suggestions. –  May 08 '11 at 08:14
  • @pat - I need to apologize myself, sorry for overreacting. That said, what you asked originally and what you asked in the above comment vary quite a bit. First of, you mentioned that it's ok to do it from scratch yourself. Secondly, you asked how to handle the verbs (which is what I answered). – Christian May 08 '11 at 15:24
  • Just want to comment on this for people coming here from google. The cases for PUT and DELETE can be implemented as Backbone sends a PUT whenever you try to save an already existing model. – Rohit Nov 11 '13 at 15:59
4

There are lots of restfull frameworks for PHP, have a look here and here.

I 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

Community
  • 1
  • 1
dwarfy
  • 3,076
  • 18
  • 22
4

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.

You 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

Charles
  • 538
  • 5
  • 9
3

new REST api solution

examples 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/

:)

Mikelangelo
  • 907
  • 8
  • 20
3

you might want to look at Slim:

http://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.