【问题标题】:I can't make Restler work我不能让 Restler 工作
【发布时间】:2012-04-11 08:41:27
【问题描述】:

我一直在尝试使用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 的问题,但找不到任何关于它的具体信息。有什么想法吗?

【问题讨论】:

    标签: php rest nginx


    【解决方案1】:

    解决我的问题:

    我把我的代码放到/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 的未来用户有所帮助。

    【讨论】:

      【解决方案2】:

      查看rewrite module,了解如何让 nginx 接受api.php/say/hello URL。

      【讨论】:

        【解决方案3】:

        根据 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;
            #}
        }
        

        希望这对其他人有所帮助。

        【讨论】:

        • 这不适用于 GET 调用,它必须像这样“尝试文件 $uri $uri/ /api/index.php?$args”。
        猜你喜欢
        • 1970-01-01
        • 2014-04-05
        • 1970-01-01
        • 1970-01-01
        • 2016-01-09
        • 1970-01-01
        • 1970-01-01
        • 2014-06-12
        • 2021-07-04
        相关资源
        最近更新 更多