|
List Info
Thread: Ajax way
|
|
| Ajax way |

|
2007-11-25 15:16:40 |
Hi,
I want to return json from an action, but I have hard times
finding out
how to do it the best way with ZF. For the moment I do:
function updateorderAction() {
$i = 1;
$o_divisiontable = new Division();
foreach($_POST['divisioncontainer'] AS $i_divisionid) {
$o_division =
$o_divisiontable->fetchRow("division_id =
$i_divisionid");
$o_division->priority = $i;
$o_division->save();
$i++;
}
$a_return = array("result", 1);
echo json_encode($a_return);
die;
}
But that isn't the way, is it?
/Peter
|
|
| RE: Ajax way |

|
2007-11-25 15:45:21 |
Peter,
This may not be the most correct way, but this is the
solution I use:
Zend_Loader::loadClass('Zend_Json');
$this->_response->setHeader('content-type',
'application/json', true);
$this->_response->setBody("/*-secure-n" .
Zend_Json::encode($response) .
"n*/");
$response really can be anything, a string, an array, an
object.
Cheers,
Steven
-----Original Message-----
From: Peter Lauri [mailto:peterlauri gmail.com]
Sent: Monday, 26 November 2007 7:17 AM
To: Zend Framework General
Subject: [fw-general] Ajax way
Hi,
I want to return json from an action, but I have hard times
finding out
how to do it the best way with ZF. For the moment I do:
function updateorderAction() {
$i = 1;
$o_divisiontable = new Division();
foreach($_POST['divisioncontainer'] AS $i_divisionid) {
$o_division =
$o_divisiontable->fetchRow("division_id =
$i_divisionid");
$o_division->priority = $i;
$o_division->save();
$i++;
}
$a_return = array("result", 1);
echo json_encode($a_return);
die;
}
But that isn't the way, is it?
/Peter
|
|
| RE: Ajax way |

|
2007-11-25 15:53:02 |
But the problem is I am getting "edit.phtml" not
found if I don't "die"
the action. Or will this change if I do the setHeader
content-type?
/Peter
On Mon, 2007-11-26 at 07:45 +1000, Steven Brown wrote:
> Peter,
>
> This may not be the most correct way, but this is the
solution I use:
>
> Zend_Loader::loadClass('Zend_Json');
> $this->_response->setHeader('content-type',
'application/json', true);
>
$this->_response->setBody("/*-secure-n" .
Zend_Json::encode($response) .
> "n*/");
>
> $response really can be anything, a string, an array,
an object.
>
> Cheers,
> Steven
>
> -----Original Message-----
> From: Peter Lauri [mailto:peterlauri gmail.com]
> Sent: Monday, 26 November 2007 7:17 AM
> To: Zend Framework General
> Subject: [fw-general] Ajax way
>
> Hi,
>
> I want to return json from an action, but I have hard
times finding out
> how to do it the best way with ZF. For the moment I
do:
>
> function updateorderAction() {
> $i = 1;
> $o_divisiontable = new Division();
> foreach($_POST['divisioncontainer'] AS $i_divisionid)
{
> $o_division =
> $o_divisiontable->fetchRow("division_id =
> $i_divisionid");
> $o_division->priority = $i;
> $o_division->save();
> $i++;
> }
> $a_return = array("result", 1);
> echo json_encode($a_return);
> die;
> }
>
> But that isn't the way, is it?
>
> /Peter
>
>
>
|
|
| Re: Ajax way |

|
2007-11-25 16:10:25 |
|
| Why don't you instead have the json encoding happen from a view action?
e.g. Assuming your controller is AjaxController.php /div>
<?php
class AjaxController extends Zend_Controller_Action { ...other code... function updateorderAction() { $i = 1; $o_divisiontable = new Division(); foreach ($this->getRequest()->getPost('divisioncontainer') AS $i_divisionid) { $o_division = $o_divisiontable->fetchRow("division_id = $i_divisionid"); $o_division->priority = $i; $o_division->save(); $i++; } $this->view->result = array("result", 1); } }
...and then your corresponding view action would be in your view folders ajax/updateorder.phtml
<?= Zend_Json::encode($this->result) ?>
(Note, I also added the 'getReques t()->getPost()' accessor to be more MVC-friendly 
Cheers
But the problem is I am getting "edit.phtm l" not found if I don't "die" the action. Or will this change if I do the setHeader content-type?
/Peter
On Mon, 2007-11-26 at 07:45 +1000, Steven Brown wrote:
Peter,
This may not be the most correct way, but this is the solution I use:
Zend_Loader::loadClass('Zend_Json');
$this->_response->setHeader('content-type', 'application/json', true);
$this->_response->setBody("/*-secure-n" . Zend_Json::encode($response) .
"n*/");
$response really can be anything, a string, an array, an object.
Cheers,
Steven
-----Original Message-----
From: Peter Lauri [ peterlauri gmail.com">mailto:peterlauri gmail.com]
Sent: Monday, 26 November 2007 7:17 AM
To: Zend Framework General
Subject: [fw-general] Ajax way
Hi,
I want to return json from an action, but I have hard times finding out
how to do it the best way with ZF. For the moment I do:
function updateorderAction() {
$i = 1;
$o_divisiontable = new Division();
foreach($_POST['divisioncontainer'] AS $i_divisionid) {
$o_division =
$o_divisiontable->fetchRow("division_id =
$i_divisionid");
$o_division->priority = $i;
$o_division->save();
$i++;
}
$a_return = array("result", 1);
echo json_encode($a_return);
die;
}
But that isn't the way, is it?
/Peter
--
Simon Mundy | Director | PEPTOLAB
""" " "" """""" "" "" """"""" " "" """"" " """"" " """""" "" "
Registration number for Simon 160725
202/258 Flinders Lane | Melbourne | Victoria | Australia | 3000 Voice +61 (0) 3 9654 4324 | Mobile 0438 046 061 | Fax +61 (0) 3 9654 4124 |
| Re: why Zend_Json::encode? (was: Ajax
way) |

|
2007-11-25 16:13:11 |
Ok, cool... But why is there a wrapper for a simple function
call for
the json, wouldn't json_encode($this->result) be simpler?
Or is there
something I don't get?
On Mon, 2007-11-26 at 09:10 +1100, Simon Mundy wrote:
> <?= Zend_Json::encode($this->result) ?>
|
|
| Re: why Zend_Json::encode? (was: Ajax
way) |

|
2007-11-25 16:21:42 |
|
Well, really only for PHP 5.1.x compatibility - primarily for lazy sods like me who rely on OS RPMs to update to PHP 5.2.x 
Ok, cool... But why is there a wrapper for a simple function call for the json, wouldn't json_encod e($this->result) be simpler? Or is there something I don't get? 
On Mon, 2007-11-26 at 09:10 +1100, Simon Mundy wrote:
<?= Zend_Json::encode($this->result) ?>
--
Simon Mundy | Director | PEPTOLAB
""" " "" """""" "" "" """"""" " "" """"" " """"" " """""" "" "
Registration number for Simon 160725
202/258 Flinders Lane | Melbourne | Victoria | Australia | 3000 Voice +61 (0) 3 9654 4324 | Mobile 0438 046 061 | Fax +61 (0) 3 9654 4124
|
| Re: why Zend_Json::encode? (was: Ajax
way) |

|
2007-11-25 16:23:54 |
What do you mean? It was the same way to call in 5.1.x,
wasn't it?
On Mon, 2007-11-26 at 09:21 +1100, Simon Mundy wrote:
> Well, really only for PHP 5.1.x compatibility
|
|
| Re: why Zend_Json::encode? (was: Ajax
way) |

|
2007-11-25 16:28:12 |
or
class AjaxController extends Zend_Controller_Action
{
function updateorderAction()
{
....
Zend_Controller_Front::getInstance()->setParam('noViewRen
derer',true);
$this->getResponse()->setBody(json_encode($this->re
sult));
}
Le Mon, 26 Nov 2007 00:13:11 +0200,
Peter Lauri <peterlauri gmail.com> a écrit :
> Ok, cool... But why is there a wrapper for a simple
function call for
> the json, wouldn't json_encode($this->result) be
simpler? Or is there
> something I don't get?
>
>
> On Mon, 2007-11-26 at 09:10 +1100, Simon Mundy wrote:
> > <?= Zend_Json::encode($this->result) ?>
>
|
|
| Re: why Zend_Json::encode? (was: Ajax
way) |

|
2007-11-25 16:30:06 |
That one seams more "light" as it wouldn't require
any rendering of the
view. Or am I incorrect? Should I set the contenttype as
well? /Peter
On Sun, 2007-11-25 at 23:28 +0100, Stephane wrote:
>
> class AjaxController extends Zend_Controller_Action
> {
>
> function updateorderAction()
> {
> ....
>
>
Zend_Controller_Front::getInstance()->setParam('noViewRen
derer',true);
>
$this->getResponse()->setBody(json_encode($this->re
sult));
> }
|
|
| Re: why Zend_Json::encode? (was: Ajax
way) |

|
2007-11-25 16:33:45 |
|
Yes, but only if you had the extension - it was built-in to 5.2
What do you mean? It was the same way to call in 5.1.x, wasn't it? On Mon, 2007-11-26 at 09:21 +1100, Simon Mundy wrote:
Well, really only for PHP 5.1.x compatibility
--
Simon Mundy | Director | PEPTOLAB
""" " "" """""" "" "" """"""" " "" """"" " """"" " """""" "" "
Registration number for Simon 160725
202/258 Flinders Lane | Melbourne | Victoria | Australia | 3000 Voice +61 (0) 3 9654 4324 | Mobile 0438 046 061 | Fax +61 (0) 3 9654 4124
|
| Re: why Zend_Json::encode? (was: Ajax
way) |

|
2007-11-25 16:35:59 |
Not built-in, but bundled, differnt Still you
have to include the
extension to make it make it work...
On Mon, 2007-11-26 at 09:33 +1100, Simon Mundy wrote:
> Yes, but only if you had the extension - it was
built-in to 5.2
|
|
| Re: Ajax way |

|
2007-11-25 17:38:39 |
Hi Peter,
> But the problem is I am getting "edit.phtml"
not found if I don't
> "die"
> the action. Or will this change if I do the setHeader
content-type?
I'm not getting into the JSON part of it but that issue just
sounds
like you've not disabled the view renderer. Just adding:
...rest of your code...
echo json_encode($a_return);
$this->_helper->viewRenderer->setNoRender();
Should stop ViewRenderer trying to auto render a view
script.
http://framework
.zend.com/manual/en/
zend.controller.actionhelpers.html#zend.controller.actionhel
per.stockhel
pers
Nick
|
|
[1-12]
|
|