symfony:在<script>标记</script>中调用url_for()时出错

时间:2011-08-11 17:26:45

标签: symfony1 symfony-1.4

我在这些javascript标签中调用url_for():

<script type="text/javascript">
    $.post('<?php echo url_for('category_set_representative_image',
array('sf_subject' => $category)) ?>',
           function(){

             alert("fadsf");
           }
    );

</script>

但是我收到了这个错误:

sfError404Exception: Empty module and/or action after parsing the URL
'/rappresentativa' (/).

我也有这个路由规则:

category_set_representative_image:
  url:     /rappresentativa
  param:   { module: category_new, action: setRepresentativeImage }
  class:   sfDoctrineRoute
  options: { model: Category, type: object }

注意:如果我调用相同的url_for(),我没有问题  标签

有什么想法吗?

sf 1.4

Javi

3 个答案:

答案 0 :(得分:2)

您在上面描述的错误与script标记无关。你在哪里使用它并不重要(url_for)。

首先,我认为您需要使用url_for(array('sf_route' => 'category_set_representative_image', 'sf_subject' => $category)) url_for('category_set_representative_image', $category)。当您需要传递的参数多于对象时,第一个参数非常有用。 More

第二次(不太重要),也许您需要在网址中添加更多参数:

category_set_representative_image:
  url:     /rappresentativa/:id
  param:   { module: category_new, action: setRepresentativeImage }
  class:   sfDoctrineRoute
  options: { model: Category, type: object }
  requirements:
    id: \d+

第三次,检查模块和操作是否确实存在:

// apps/<application>/category_new/actions.class.php
public function executeSetRepresentativeImage(sfWebrRequest $request) {
  $category = $this->getRoute()->getObject();

希望这会有用。

答案 1 :(得分:0)

我认为这个问题只与javascript和PHP之间滥用引号有关。 试试吧:

<script type="text/javascript">
$.post('<?php echo url_for("category_set_representative_image",
array("sf_subject" => $category)) ?>',
       function(){

         alert("fadsf");
       }
);

答案 2 :(得分:0)

This确实有效:

category_set_representative_image:
  url:     /rappresentativa
  param:  { module: category_new, action: setRepresentativeImage}
  class:   sfDoctrineRoute
  options: { model: Category, type: object }
  requirements: { sf_method: post }

编辑:无论如何,antonkalyaev说:

category_set_representative_image:
  url:     /rappresentativa/:id
  param:  { module: category_new, action: setRepresentativeImage}
  class:   sfDoctrineRoute
  options: { model: Category, type: object }
  requirements: { sf_method: post }

哈维