-- Rishi Daryanani <[EMAIL PROTECTED]> wrote (on Wednesday, 04 June 2008, 07:50 PM -0700): > I am using the ZF mvc pattern but a complete newbie, > and using my own code style in a lot of areas of my > site. I would like to make a simple ajax-based > functionality (using Prototype JS's "AjaxUpdater > function) . All I need to do is: > > i) Write my code for the main page which will display > a div id which will be used for the ajax result (I can > do this from the view/controller scripts) > > ii) use AjaxUpdater to call the url > "http://mydomain.com/myscript.php" and return the > result in the above div > > iii) Write the code for myscript.php to simply return > a result as pure HTML > > The problem is, if I try to access > http://mydomain.com/myscript.php, it will not actually > go to the script, but instead ZF will look for > controllers and views of this script and try to > generate a new html page with the site's usual headers > and footers. This is because my .htaccess file has > been set up as described in the ZF newbie > documentation. However, I do not want html headers and > footers returned back..I simply want myscript.php to > return the html it is supposed to return... and > nothing else..as this html will be placed in the div > above. > > So, the solution here is to create a public directly > called "my_direct_scripts" or something and store > myscript.php in that. I will then modify the .htaccess > file to EXCLUDE this directory from redirection, so > that scripts within it can be accessed directly. Then > my ajax function should work. > > I just want to make sure I'm doing this the right way, > or if anyone would recommend something different?
You have several options. First, you can modify your .htaccess to exclude certain scripts. One common variant on the RewriteRules used is as follows: RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule !\.(js|ico|gif|jpg|png|css)$ /index.php This basically says, "if the file exists, or the directory exists, don't rewrite." Another option is to return your AJAX payloads via your ZF apps. This is actually a really good idea, as it keeps application logic in one location -- typically even the same controller. When using this method, though, you need to ensure that the results do not include other layout artifacts, as you noted before. You can do this by disabling rendering and/or layouts when an AJAX request is detected: if ($this->getRequest()->isXmlHttpRequest()) { // disable layout: $this->_helper->layout()->disableLayout(true); // don't render view script automatically: $this->_helper->viewRenderer->setNoRender(true); ... } The above is largely taken care of if you choose to use the AjaxContext view helper, btw -- which is the third option I can present. Search the manual for AjaxContext and/or ContextSwitch to find more information on those action helpers and how they can assist AJAX requests. -- Matthew Weier O'Phinney Software Architect | [EMAIL PROTECTED] Zend Framework | http://framework.zend.com/