I think this is a great approach and I do something very similar but I
also throw an indicator in the url so it is very easy to recognize
service type call vs a normal output page. For example
http://mydomain.com/Xsearch/now the capital X is very easy to know right
off the bat that this is could be json, html xml etc. etc.... Nice when
looking at your controllers too.
Brian
back-2-95 wrote:
I used the $this->_request->isXmlHttpRequest() to determine if request is
made with ajax. This works with jQuery. Then I have made following rows to
be called automatically, when $this->_request->isXmlHttpRequest() is true.
$this->getFrontController()->setParam('noViewRenderer', true);
$this->getResponse()->setHeader('Content-type', 'text/html');
Now I can decide freely what kind of response to send to ajax-function.
Json, html, xml...
I can use normally $this->view object and render different view script when
called from ajax.
example from some action method:
when this is called from ajax, viewRenderer won't auto-render (made in
preDispatch).
I change the view script to be rendered at the bottom of method (when called
from ajax).
If this action is called directly from browser, it will auto-render view
script with default filename (/view/scripts/{controller}/{action}.phtml
public function detailAction()
{
$this->view->title =
"File_ImageController::detailAction";
$this->view->src = "/image/src/$this->_filename";
$this->view->width = $this->_width;
$this->view->height = $this->_height;
$this->view->download = "/download/$this->_filename";
if ($this->_request->isXmlHttpRequest())
{
$this->render('detail-ajax-version'); //
}
}
I hope this helps you or somebody...
ZF rocks!