Setting up a RESTful service for Backbone.js using apache and windows

I am trying to set up a RESTful web service on my apache localhost to serve as background content for my base application. I tried:

  • Configure WebDAV, but get the following log error messages

    [Thu Feb 23 21:46:17 2012] [error] [client 127.0.0.1] Unable to set new content for / clusters / 19. [403, # 0], referent: http: //ideas.localhost/ [Thu Feb 23 21:46:17 2012] [error] [client 127.0.0.1] An error occurred while opening the resource. [500, # 0], referent: http: //ideas.localhost/

  • Using Backbone.emulateHTTP, which causes 405 method not allowed error(something, I think, is caused by the header X-HTTP-Method-Override: PUT, as normal POST requests work fine

I am running Apache 2.2.21 and PHP 5.3 on Windows 7, and below is the .htaccess file. I also use the SLIM framework to handle URL routing.

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

And virtual host configuration

<VirtualHost *:80>
    DocumentRoot "G:/sites/ideas"
    Dav On // I also had security setting set to Allow all as it just my localhost
    ServerName ideas.localhost
    ErrorLog "logs/ideas.localhost-error.log"
    CustomLog "logs/ideas.localhost-access.log" combined
    SetEnv APPLICATION_ENV development
</VirtualHost>

I have been struggling to get something to work for ages, so any help is greatly appreciated.

+5
source share
1 answer

I can’t believe that I solved the problem less than an hour after the discovery of generosity, but hey ho.

The problem was that Slim had no built-in ability to handle the header X-HTTP-Method-Overrideused by the backbone, and the error message was not very descriptive. By adding the following to the bottom of request.php and using emulateHTTP mode in Backbone, he fixed it

protected function checkForHttpMethodOverride() {
    if ( isset($this->post[self::METHOD_OVERRIDE]) ) {
        $this->method = $this->post[self::METHOD_OVERRIDE];
        unset($this->post[self::METHOD_OVERRIDE]);
        if ( $this->isPut() ) {
            $this->put = $this->post;
        }
    } else if(isset($this->headers['x-method-override'] )) {
        $this->method = $this->headers['x-method-override'];
        if ( $this->isPut() ) {
            $this->put = $this->post;
        }
    }
}

PS - pull request SLIM, , , , , ,

+4

All Articles