A RESTful persistence solution usable with backbone.js… in PHP?

旧街凉风 提交于 2019-11-27 17:32:38

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

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.

dwarfy

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

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!