Cheat symfony


Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Cheat symfony

  1. 1. Open-Source PHP5 MVC Framework Agile Development Helpers PART 1 - JAVASCRIPT(JS) and AJAX (remote calls) <?php echo use_helper('Javascript') ?> JAVASCRIPT AND AJAX HELPERS REMOTE CALL PARAMETERS All the AJAX helpers can take other parameters, in addition to JavaScript Helpers the update and url parameters: link_to_function ($name, $function, $html_options=array()) position <?php echo link_to_function("Click me!", "alert('foobar')") ?> // will generate: The position parameter can be defined as: <a href="#" onClick="alert('foobar');return none;">Click me!</a> Value Position javascript_tag ($content) before before the element <?php echo javascript_tag("document.getElementById('indicator').innerHTML= after after the element '<strong>Data processing complete</strong>';") ?> top at the top of the content of element bottom bottom of the content of element update_element_function ($element_id, $options=array()) <?php echo javascript_tag( update_element_function( ‘indicator', array( conditions "position"=>"after", "content" =>"<strong>Data processing complete</strong>" )))?> confirm 'confirm' => 'Are you sure?’ Ajax Helpers A JS dialog box showing 'Are you sure?' will pop-up when An AJAX interaction is made up of three parts: the user clicks on the caller, and the module/action will be * a caller (a link, button or any control that the user manipulates to launch the action) called only if the user confirms his choice by clicking 'Ok'. * a server action * a zone in the page to display the result of the action to the user. condition Symfony provides multiple helpers to insert AJAX interaction in your templates by 'condition' => "$('elementID') == true", putting the caller in a link, a button, a form, or a clock. These helpers output HTML The remote call can also be conditioned by a test code, not JavaScript. performed on the browser side (in JavaScript). link_to_remote ($name, $options=array(), $html_options=array()) script execution <?php echo link_to_remote('Delete this post', array( 'script' => true 'update' => 'indicator', 'url' => 'post/delete?id='.$post->getId() )) ?> If the response code of the AJAX call (the code sent by the server, inserted in the update element) contains JS, remote_function ($options=array()) these scripts are not executed by default. This is to <?php echo javascript_tag(remote_function(array( prevent remote attack risks.The ability to execute scripts change part of the in remote responses explicitly with the script option. 'update' => 'myzone', page according to 'url' => 'mymodule/myaction' a server response callbacks ))) ?> Callback Event form_remote_tag ($options=array(), $options_html=array()) before Before request is initiated <?php echo form_remote_tag(array( after Immediately after request is initiated and before loading 'update' => 'item_list', 'url' => '@item_add' )) ?> opens a <form>, just like loading When the remote response is being loaded <label for="item">Item:</label> the form_tag() helper loaded When the browser has finished loading the <?php echo input_tag('item') ?> does. remote response <?php echo submit_tag('Add') ?> interactive When the user can interact with the remote </form> response, even though it has not finished loading observe_field ($field_id, $options=array()) success When the XMLHttpRequest is completed, and the HTTP status code is in the 2XX range <?php echo form_tag('@item_add_regular') ?> the module/action written in the failure When the XMLHttpRequest is completed, and <label for="item">Item:</label> @item_being_typed rule will be the HTTP status code is not in the 2XX range <?php echo input_tag('item') ?> called each time the item field 404 When the request returns a 404 status <?php echo submit_tag('Add') ?> changes, and the action will be complete When the XMLHttpRequest is complete <?php echo observe_field('item', array( able to get the current item value (fires after success or failure, if present) from the value request parameter. 'update' => 'item_suggestion', If you want to pass something else e.g.: <?php echo link_to_remote('Delete this post', array( 'url' => '@item_being_typed' than the value of the observed field, 'update' => 'indicator', )) ?> you can specify it as a JavaScript 'url' => 'post/delete?id='.$post->getId(), </form> expression in the ‘with’ parameter 'position' => 'after', ‘confirm' => 'Are you sure?', periodically_call_remote ($options=array()) 'script' => true <?php echo periodically_call_remote(array( 'loading' => "'indicator')", this helper is an AJAX 'complete' => "Element.hide('indicator')" )) ?> 'frequency' => 60, interaction triggered every x 'update' => 'notification', seconds. It is not attached to a 'url' => '@watch', HTML control, but runs NOTES 'with' => "'param=' + $('mycontent').value" transparently in the background, as a behaviour of the whole page * Actions called as remote functions know that they are in )) ?> an AJAX transaction, and therefore automatically don't Another functions include the web debug toolbar in development. Also, they submit_to_remote ($name, $value, $options=array()) skip the decoration process (their template is not included evaluate_remote_response () in a layout by default). If you want an Ajax view to be observe_form ($form_id, $options=array()) decorated, you need to specify explicitly has_layout: true visual_effect ($name, $element_id=false, $js_options=array()) for this view in the module view.yml file. Actions called sortable_element ($element_id, $options=array()) through Ajax, return true to the following call: draggable_element ($element_id, $options=array()) $isAjax = $this->isXmlHttpRequest(); drop_receiving_element ($element_id, $options=array()) javascript_cdata_section ($content) * The AJAX helpers won't work if the URL of the remote input_auto_complete_tag ($name, $value, $url, $tag_options=array(), action doesn't belong to the same domain as the current $completion_options=array()) page. This restriction exists for security reasons, and relies input_in_place_editor_tag ($name, $url, $editor_options=array()) on browsers limitations that cannot be bypassed. The Prototype and JavaScript libraries are bundled with the symfony. This cheat-sheet is not an official part of the symfony documentation