Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC: how to ajax?

I'm going to start a project using a Zend Framework MVC implementation.

How do I work with ajax? I mean, should I place all ajax code into controller? Or into view?

For example, I want to get posts from author 'ivan' to show on a page.

Normally, I create a link to '/posts/author/ivan' or smth like it, create a new Action like 'byAuthorAction()' in the Posts controller (or maybe Users controller, wherever), view for it (and all code what gets posts from model goes there) and create a new Route to it.

How to add functionality to get any user's posts in json, xml formats for ajax and maybe API, keeping the DRY principle and designing the code structure as smart as I can?

--

Thanks for answers! I will be very glad to see comments about designing MVC in situations like mine. I have some experience in basic MVC principles but not in more complicated cases. Maybe some useful links?

like image 273
Valentin Golev Avatar asked Sep 30 '09 16:09

Valentin Golev


People also ask

Where do I put jQuery code in MVC?

After the ASP.NET MVC Framework is installed a new ASP.NET MVC Web Application project should be created. Next, download jQuery, get the Packed or Minified version and place it into the Content folder of the the new web application project. Add a reference to the jQuery file put in the Content folder.


1 Answers

You really should read the manual chapter about ContextSwitch Action Helper. But here is a brief outline:

  • your view scripts (action-name.phtml) are used for regular HTML output
  • you can initialize a context switch for some actions in the controller so thay can output for example XML - xml context is supported by default and you would put your view script for xml context in (action-name.xml.phtml); xml context also disables rendering of the layout
  • json is also supported by the built in context switch and default option is to disable both the layout and the view and to output all the variables assigned to the view from the controller action in JSON format, this option can be toggled by using the setAutoJsonSerialization(false) method of the context switch; but if you switch it you have to create a view script action-name.json.phtml and output the variables in JSON format by hand

To switch between these two contexts you have to add a format parameter to your URL, e.g. /posts/author/ivan/format/json or /posts/author/ivan/format/xml. If you do not specify the format your application will output plain html.

Special version of the Context switch is AjaxContext and you also have to configure this one by hand. It does not use the 'format' parameter to identify which format it should use for output but it examines the header sent in your request and looks for 'X-Requested-With: XmlHttpRequest' header and if it is present the AjaxContext is examined. Using the AjaxContext action helper you can specify which context should be used for specific actions if the request is fired using AJAX.

like image 167
Goran Jurić Avatar answered Nov 13 '22 06:11

Goran Jurić