2012-10-10 29 views
2

我有一個非常簡單的類,它派生自Restler網站上給出的示例「Say」類​​。這是因爲如下:Restler 3 - 參數的默認值阻止了參數傳遞的能力

<?php 
class Say { 
    function hello($to='world') { 
    return "Hello $to!"; 
    } 
    function hi($to) { 
    return "Hi $to!"; 
    } 

    function nothing($to='the ground') { 
    return "Looks at {$to} quietly but without saying a word."; 
    } 
} 

因爲「喜」功能沒有爲$變量的默認值它在很大程度上做了它應該:

http://localhost/api/say/hi/Jack 

回報

嗨,傑克!

大。問題是,當你有像「你好」或「無」功能的默認值的話,好像你不能在參數傳遞了:

http://localhost/api/say/hello   -- WORKS, returns "Hello world!" 
http://localhost/api/say/hello/Jack  -- FAILS, returns a JSON error of 404 

任何幫助將不勝感激。

在一個側面說明,我也注意到,如果你不使用帶有「喜」的參數(這需要$設置爲東西),它會返回404錯誤也是如此。我不確定這是否是預期的行爲,但它似乎是這種錯誤的錯誤信息。

回答

3

Restler 2的工作完全像你期望的那樣,對於上述hello方法會生成以下途徑

GET say/hello  ⇠ Say::hello() 
GET say/hello/{to} ⇠ Say::hello() 

但Restler 3只讓一個路線

GET say/hello ⇠ Say::hello() 

這是因爲智能路由發生,在這裏我們不映射可選參數的URL,所以可選參數可以作爲查詢字符串

在喜方法Restler 2點的情況下的路線被傳遞它作爲

GET say/hi  ⇠ Say::hi() 
GET say/hi/{to} ⇠ Say::hi() 

凡爲Restler 3

GET say/hi/{to} ⇠ Say::hi() 

因此使得say/hi失敗因爲缺少必需的參數

這樣做的目的是爲了避免歧義。在此解釋了routing example

如果你想爲你的Restler 3 API Restler 2行爲添加下面的index.php

Defaults::$smartAutoRouting = false; 

如果你只是想關閉的方法智能自動路由級或類級別,添加以下PHP文檔註釋

@smart-auto-routing false