user274383
user274383

Reputation: 121

Zend Framework :: Ajax Requests

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

Upvotes: 3

Views: 1868

Answers (4)

Namron Regrebsubla
Namron Regrebsubla

Reputation: 3

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.

Upvotes: 0

Hammad Tariq
Hammad Tariq

Reputation: 1523

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/

Upvotes: 1

opHasNoName
opHasNoName

Reputation: 20736

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

Upvotes: 2

Grant Collins
Grant Collins

Reputation: 1791

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

Upvotes: 1

Related Questions