我不能让Restler工作

时间:2012-04-11 08:41:27

标签: php rest nginx

我一直在尝试使用Restler php api。

我的网络服务器根目录下有一个api.php文件:

<?php
require_once('lib/restler.php');

class Say {
    function hello($to='world') {
        return "Hello $to!";
    }
}

$r = new Restler();
$r->addAPIClass('Say');
$r->handle();
?>

我尝试使用最简单的示例GET api.php / say / hello,但是nginx继续回复我的错误404.在日志中我看到“不是目录”。

我想这是我的nginx conf的问题,但无法找到有关它的任何具体信息。有什么想法吗?

3 个答案:

答案 0 :(得分:5)

解决我的问题:

我将我的代码放入/api/index.php

然后修改我的nginx配置添加:

location /api {
        if (!-f $request_filename) {
            rewrite ^(.*)$ /api/index.php last;
        }

        if (!-d $request_filename) {
            rewrite ^(.*)$ /api/index.php last;
        }
    }

所以现在如果我调用GET / api / say / hello,它会重定向到/api/index.php/say/hello,重写规则允许restler发挥其魔力。

我希望这将有助于nginx上的restler的未来用户。

答案 1 :(得分:1)

查看rewrite module,了解如何使nginx接受api.php/say/hello网址。

答案 2 :(得分:0)

经过前面的考虑,即使解决方案有效,根据nginx文档 - (IfIsEvil http://wiki.nginx.org/IfIsEvil) - 您应该能够在没有if的情况下执行此操作:

location /api {
    try_files $uri /api/index.php;

    #if (!-f $request_filename) {
    #    rewrite ^(.*)$ /api/index.php last;
    #}

    #if (!-d $request_filename) {
    #    rewrite ^(.*)$ /api/index.php last;
    #}
}

希望这有助于其他人。

相关问题