2011-11-20 82 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实现。

所有被记录得非常好: