开发者

Zend Framework :: Ajax Requests

开发者 https://www.devze.com 2022-12-24 10:46 出处:网络
I am looking out for any library that would facilitate Ajax in Zend (if any exist). Also 开发者_开发技巧can somebody throw some light on the built-in Ajax support that comes with ZF.

I am looking out for any library that would facilitate Ajax in Zend (if any exist). Also 开发者_开发技巧can somebody throw some light on the built-in Ajax support that comes with ZF. I tried googling but was not satisfied with the results.

-DevD


altough JQuery is well integrated with the Zend Framework. There is an libray inside the /extras Folder.

There are helpers for AjaxRequests, different View Widgets, and for loading the Library from Google/AOL CDN.

For more information i would suggest to visit the Zend Framework Documentation for :

ZendX Jquery


Dojo is shipped with the Zend Framework and they facilitate ajax style calls.

If you have a look at Dojo ToolKit to find out more about the what that framework can provide you, these include the ajax calls (search for xhrGet and xhrPost)

Also if you look here in the framework documentation to help you use dojo in your Zend Framework Project. Zend_Dojo

Hope this helps


If you have any expertise in YUI JS framework, it is really easy to listen to the events and make Ajax calls and to collect your elements through Selector query and then apply CSS rules on them (if that is what you want). You can have a look at this tutorial to understand more about Zend-YUI relationship

http://ciitronian.com/blog/programming/javascript/creating-ajax-based-form-zend-framework-yui/


Take a look at this Zend Framwork 2 module.

If you do not have a application you can use the Wasabilib Skeleton https://github.com/WasabiLib/wasabilib_zf2_skeleton_application. It comes with all necessary assets in the right place.

If you already have an application you should only clone the module not the full skeleton.

Minimal requirements: jQuery, ZF2

  1. Add the module to application.config.php.
  2. Include the wasabilib.min.js after jquery in the head of your layout.phtml

How it works in your .phtml-file you have a form like this:

<form id="simpleForm" class="ajax_element" action="simpleFormExample" method="POST">
<input type="text" name="written_text">
<input type="submit" value="try it">
</form>

Anywhere else in your phtml you can place an element where the response is shown.

In your Controller the following method:

public function simpleFormExampleAction(){
    $postArray = $this->getRequest()->getPost();
    $input = $postArray['written_text'];
    $response = new Response(new InnerHtml("#element_simple_form","Server     Response: ".$input));
    return $this->getResponse()->setContent($response);
}

The form has a class "ajax_element" this will say the the library that the request will be done with an xmlhttp-request. It wont work if you do not give an id to the requesting element. So the form has the ID "simpleForm". The action is the "path/to/controller" just like a normal request.

In the controller action a new WasabiLib\Ajax\Response object is instanciated. The InnerHtml class is for replace, prepend and append html or normal text to a selector. In this case the selector is an ID "element_simple_form". The first parameter of the InnerHtml class is the selector. Make sure that you write #yourElementId or .yourClassSelector. For IDs an "#" and for class selectors "."

The second parameter is the Text you want to fill in this element.

The response object can handle a lot more responses which you can add with

$response->add($anotherResponseType);

A list of possible response types is on its homepage wasabilib.org

The module is build to handle ajax request an responses in a very simple way. Once you have understood the behavior you can handle almost every practical ajax need.

0

精彩评论

暂无评论...
验证码 换一张
取 消