2012-04-11 202 views
0

我一直在嘗試使用Restler php api。我無法讓Restler工作

我在我的web服務器的根目錄下有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 /說/打招呼,但nginx的不斷響應我有一個錯誤404。在日誌中我看到「不是目錄」。

我想這是我的nginx conf的問題,但找不到任何關於它的具體信息。任何想法?

回答

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的未來用戶。

+0

優秀 - 感謝您節省我的時間! – Ross 2013-04-14 23:02:54

+0

男人,你搖滾! – 2013-10-04 14:18:57

1

調查rewrite module以瞭解如何讓nginx接受api.php/say/hello的網址。

+0

這就是問題所在。花了一些時間來解決它。 – Bokal 2012-04-14 12:40:45

0

前考慮後,即使解決方案的工作,按照nginx的文檔 - (IfIsEvil http://wiki.nginx.org/IfIsEvil) - 你應該能夠做到這一點,而不要是如下:

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

希望這會幫助別人。

+0

這對GET調用不起作用,它必須像這樣「試試$ uri $ uri//api/index.php?args」。 – Nemke 2015-02-04 14:15:50