AjaxContext with the MVC separation

For programming and general questions on Zend Framework

AjaxContext with the MVC separation

Postby marcoart on Tue Apr 12, 2011 10:20 am

Hi guys!

i'm trying tu use the AjaxContent respond at the ajax request in MVC pattern.

now, i call loginAction (where i correctly use a models and get connection into db) from LoginController.
I'm following "Zend Framework in Action" in this way:

LoginController:
Code: Select all
public function init()
{
$ajaxContext = $this->_helper->getHelper('AjaxContext');
$ajaxContext->addActionContext('login', 'json');
$ajaxContext->initContext();
}

public function loginAction(){
......
......
$this->_view->login = $response;
}


now i try the get this response into /application/views/login/login.ajax.json.phtml :
Code: Select all
<?php echo this->login; ?>


but this is not work correctly!
how can use it? wich folder should i put inside this login.ajax.json.phtml?

thanks!
Best Regards
______________________________________________
Ristoranti Roma

e-mail: info@ristorantiromaristo.it
http://www.ristorantiromaristo.it
marcoart
 
Posts: 25
Joined: Thu Feb 17, 2011 12:09 pm
Location: Roma

Re: AjaxContext with the MVC separation

Postby delboy1978uk on Wed Apr 13, 2011 10:13 am

/application/views/login/login.ajax.json.phtml

that shouldn't be the filename!
isnt it just login.phtml?
or json.phtml?
or ajax.phtml?

whats the name of the action?
delboy1978uk
 
Posts: 28
Joined: Tue Sep 21, 2010 1:27 am

Re: AjaxContext with the MVC separation

Postby delboy1978uk on Wed Apr 13, 2011 10:15 am

just read your code, its loginAction()
the corresponding view would be
/application/views/login/login.phtml
delboy1978uk
 
Posts: 28
Joined: Tue Sep 21, 2010 1:27 am

Re: AjaxContext with the MVC separation

Postby marcoart on Fri Apr 15, 2011 8:41 am

i'm agree that correspondig view is usually
/application/views/login/login.phtml (same name action, in folder with same name controller).

Now i'm reading "Zend Framewrk in Action" (Rob Allen writer)
and in chapter 5 he say:
The associated view file, views/scripts/check.ajax.phtml, contains just one line of
code to output the result:

Code: Select all
public function checkAction()
{
include ('models/ajax.php');
$name = trim($this->getRequest()->getParam('name'));
$this->_view->result = checkUsername($name);
}


<?php echo $this->result; ?>

The view script file has a different filename because it is an Ajax response, so the
AjaxContext action helper has changed the filename to include “ajax” to differentiate
it from the standard view script file that would be used. Other filename extensions are
.json.phtml for JSON responses and .xml.phtml for XML ones.

is there something that i haven't understand?
thanks
Best Regards
______________________________________________
Ristoranti Roma

e-mail: info@ristorantiromaristo.it
http://www.ristorantiromaristo.it
marcoart
 
Posts: 25
Joined: Thu Feb 17, 2011 12:09 pm
Location: Roma

Re: AjaxContext with the MVC separation

Postby marcoart on Fri Apr 22, 2011 12:46 pm

same helps?

thanks
Best Regards
______________________________________________
Ristoranti Roma

e-mail: info@ristorantiromaristo.it
http://www.ristorantiromaristo.it
marcoart
 
Posts: 25
Joined: Thu Feb 17, 2011 12:09 pm
Location: Roma

Re: AjaxContext with the MVC separation

Postby travesty3 on Wed Jan 23, 2013 5:56 pm

This thread is old, but I thought this question should be answered for anyone else who stumbles across this post like I did.

If you want your response back in JSON format, your controller looks fine, except that you should remove the underscore from $this->_view. It should be:
Code: Select all
$this->view->login = $response;

Next, you need to make sure you pass in the format in the GET request, so your URI might look like "/login?format=json" or "/login/format/json".

Finally, calling it this way will disable the layout and view script altogether, and Zend will automatically output any properties of the $this->view object for you, in JSON format.

Disclaimer: I'm not a Zend expert. I just started using Zend about a month ago. This is one of the things I've figured out. If it's not 100% technically accurate, please don't hate me. :-)
travesty3
 
Posts: 2
Joined: Fri Dec 10, 2010 6:04 pm


Return to Zend Framework

Who is online

Users browsing this forum: No registered users and 6 guests