0
votes

I'm calling url_for() inside these javascript tags:

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

             alert("fadsf");
           }
    );

</script>

but I'm getting this error:

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

I have also this routing rule:

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

NOTE: I don't have problems if I call the same url_for() far from the tags.

Any idea?

sf 1.4

Javi

3

3 Answers

2
votes

The error you've described above not related to the script tags. It's don't matter where do you use it (url_for).

First, I think, you need to use url_for(array('sf_route' => 'category_set_representative_image', 'sf_subject' => $category)) OR just url_for('category_set_representative_image', $category). The first is useful when you need to pass more arguments than just the object. More

Second (less important), maybe you need to add more params to your URL:

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

Third, check that module and action really exists:

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

Hope this will be useful.

0
votes

I think that the problem is just related to misusage of quotes between javascript and PHP. Try it like that :

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

         alert("fadsf");
       }
);

0
votes

This does work:

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

EDIT: anyway is also necessary what antonkalyaev says:

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

Javi