使用apache和windows为Backbone.js设置RESTful服务

whe*_*hys 5 php apache rest slim backbone.js

我正在尝试在我的apache localhost上设置一个RESTful Web服务,作为我的骨干应用程序的后端.我试过了:

  1. 设置WebDAV,但在日志中收到以下错误消息

    [Thu Feb 23 21:46:17 2012] [错误] [client 127.0.0.1]无法为/ clusters/19设置新内容.[403,#0],referer:http://ideas.localhost/ [Thu Feb 23 21:46:17 2012] [error] [client 127.0.0.1]打开资源时发生错误.[500,#0],referer:http://ideas.localhost/

  2. 使用Backbone.emulateHTTP,这会导致405 method not allowed error(我认为由于X-HTTP-Method-Override: PUT标头引起的正常POST请求正常工作

我在Windows 7上运行Apache 2.2.21和PHP 5.3,下面是我的.htaccess文件.我也在使用SLIM框架来处理url路由.

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Run Code Online (Sandbox Code Playgroud)

和虚拟主机配置

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

我一直在努力工作多年,所以任何帮助都非常感激.

whe*_*hys 4

不敢相信我在打开赏金后不到一个小时就解决了问题,但是嘿嘿。

问题是 Slim 没有内置的能力来处理X-HTTP-Method-Override主干使用的标头,并且错误消息的描述性不强。在 request.php 底部添加以下内容并在 Backbone 中使用 emulateHTTP 模式修复它

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

PS - 我已经为 SLIM 创建了一个拉取请求,默认包含此内容,因此,如果您认为将其包含在框架中是个好主意,请在那里发表评论