Placeholders in symfony's url_for() helper function
I'm working on an AJAX开发者_Python百科y autocomplete widget. I'm trying to create a symfony URL with a placeholder, which I can then pass to my Javascript, so that the JS can inject the ID of records it has retrieved via AJAX. For example:
$this->widgetSchema['sons_list'] = new ynWidgetAjaxAutocomplete(
array(
'item_url' => url_for( 'person/edit?id=%' ),
// OR
'item_url' => url_for( 'person/%/edit' ),
)
);
But neither of these works - I am looking for /person/%25/edit
, but the first yields /person/edit/action?id=%25
and the second yields /person/%25/action
. It does work if I pass a placeholder of digits, but this seems like a narsty hack to me:
$this->widgetSchema['sons_list'] = new ynWidgetAjaxAutocomplete(
array(
'item_url' => url_for( 'person/edit?id=999999999999' ),
)
);
Anyone know a cleaner way? Thanks!
If you want to pass only one parameter, you can probably use url_for('person/edit?id=')
and then just append dynamic parameter with js.
update
Another way - how about replacing %25
with js?
The main problem was that the auto-generated action was using $this->getRoute()->getObject()
, but symfony wasn't able to resolve the URL to sfDoctrineRoute
and was stuck with sfRoute
, which doesn't have a getObject()
method. I have now adjusted my action to retrieve the record based on the id
parameter, and started using url_for( 'person/edit' )
, appending ?id=123
in the JS, similar to Darmen. I am satisfied with this.
Good question! Here is my solution (pass to url number and replace it it JS)
function preview_document(NODE, TREE_OBJ) {
var url_to = '<?php echo url_for(sprintf('@document_preview?id=%s', '-999999'));?>';
$.get(url_to.replace('-999999', $(NODE).attr('id').replace('phtml_', '')), function (data) {
$('#document-viewer').html(data);
});
}
精彩评论