2011-12-07 76 views
0

我有一個具有函數admin_getcontents的ImportController。cakePHP傳遞URL作爲參數

function admin_getcontents($url = null) 
{ 
    $contents = json_decode(file_get_contents(($url)),true); 
    //some stuff 
} 

通過Ajax我打電話/管理/進口/ getcontents /有:

$.get('/admin/import/getcontents/'+ encodeURIComponent($('#urlcheck').val()) ,function(data) { 
     $('#importtable').html(data); 
     $('#busy-indicator').fadeOut('high'); 
}); 

所以我所說的頁面:/管理/進口/ getcontents/HTTP%3A%2F%2Flocalhost%2Feclipse% 2Fanarxeio%2Fexport%2Fcontents%2F

Apache給我一個404錯誤。如果我打電話/ admin/import/getcontents/1,頁面加載正確。所以我想通過一個? ?

管理/進口/ getcontents/HTTP%3A%2F%2Flocalhost%2Feclipse%2Fanarxeio%2Fexport%2Fcontents%2F

現在我沒有得到一個404錯誤,但$網址PARAM:像之前的參數在admin_getcontents()中爲空。我如何實現上述目標?

感謝

回答

0

速戰速決將三重URL編碼您的網址:

// javascript 
$.get('/admin/import/getcontents/'+ encodeURIComponent(encodeURIComponent(encodeURIComponent($('#urlcheck').val()))) ,function(data) { 
    $('#importtable').html(data); 
    $('#busy-indicator').fadeOut('high'); 
}); 

然後URL解碼它在你的PHP在使用它之前:

// php 
function admin_getcontents($url = null) 
{ 
    $url = urldecode(urldecode($url)); 
    $contents = json_decode(file_get_contents(($url)),true); 
    //some stuff 
} 

編輯以下意見:

您將需要設置您的路由來傳遞url參數。看着你的設置,它應該看起來像:

Router::connect('/admin/import/getcontents/:url', array(
    'controller' => 'import', 
    'action'  => 'getcontents', 
    'admin'  => true), 
    array(
    'url' => '(.*)', 
    'pass' => array('url') 
) 
); 
+0

沒有這不起作用。問題是cakePHP,因爲我可以看到$ _REQUEST在其數組中有url。但cakePHP不識別函數參數。 – gong

+0

哦,對不起,我以爲你已經正確設置了你的路線。我測試過了,它確實有效。讓我更新我的答案。 –

+0

另外你說你得到404嗎?如果你得到404,你怎麼看到請求對象? –