2011-11-20 33 views
0

我使用UrlHelper執行刪除操作,就像這樣:阿賈克斯用symfony UrlHelper

<?php echo link_to('delete', '@foo_delete?id=' . $foo->id, array('method'=>'DELETE'))?>

產生各種神奇的JavaScript輸出,和它的作品。

現在我想知道,有沒有簡單的方法來使用UrlHelper做同樣的事情ajax風格?

謝謝!

回答

1

從Symfony的1.0 link_to_remote被廢棄。 所以不是,你可以安裝sfJqueryReloadedPlugin並使用jq_link_to_remote()

<?php use_helper('jQuery'); 

<?php echo jq_link_to_remote('delete', array(
    'url' => '@foo_delete?id=1', 
    'confirm' => 'Are you sure?', 
    'csrf' => 1, 
    'method' => 'delete')) ?> 

但是,有這個問題:sfJqueryReloadedPlugin不支持DELETE方法,所以你可以做的是插件的微小變化。在plugins/sfJqueryReloadedPlugin/lib/helper/jQueryHelper.php在您閱讀:

if ((isset($options['method'])) && (strtoupper($options['method']) == 'GET')) $method = $options['method']; 

應該是:

if ((isset($options['method'])) && (strtoupper($options['method']) == 'GET' || strtoupper($options['method']) == 'DELETE')) $method = $options['method']; 

(只是提出這種變化的插件開發)

0

是 - 可以;-)

這裏是爲您的操作簡單sceleton:

/* is it an AJAX request ? */ 
if($request->isXmlHttpRequest()){ 
    /* do what you want to do */ 

    /* Text as result */ 
    return $this->renderText("All worked well with ajax"); 
} 

當然,你可以使用JSON作爲回報,特定的HTTP標頭或其他一些改進。這取決於你的JS實現。

所有被記錄得非常好: