6

In controller I am generating a special form by ID, passed from AJAX. Form output is JSON. Form creates finely. But my problem is to show this JSON in view. How?

Thank you.

5 Answers 5

23

In controller (http://framework.zend.com/manual/en/zend.controller.actionhelpers.html#zend.controller.actionhelpers.json):

$this->getHelper('json')->sendJson(array(
    'param1' => 'v1'
    'param2' => 'v2'
));

In view (http://framework.zend.com/manual/en/zend.view.helpers.html#zend.view.helpers.initial.json):

<?php
echo $this->json(array(
    'param1' => 'v1'
    'param2' => 'v2'
));
?>
Sign up to request clarification or add additional context in comments.

Comments

2

json is a encoded string containing vars in js style if you need to access the member in this string you need to json_decode the string so

$result = json_decode($jsonString);

but note that json treat php associative array like php object ... so if you pass an array you can access it as $result->memberReference not $result['memberReference'];

1 Comment

..unless of course you just specify true as parameter number 2: json_decode($jsonString, true), as it'll then return an array instead of an stdObject.
1

The easiest way is to stop view from being executed:

function jsonAction () {
    ....
    print $json;
    exit;
}

Also see check http://pl.php.net/json_encode if you don't have JSON string already.

Comments

1

You can use Zend class

$sData = Zend_Json::encode($aArray);

Or you can use advanced scenario like:

$data = array(
  'onClick' => new Zend_Json_Expr('function() {'
   . 'alert("I am a valid javascript callback '
   . 'created by Zend_Json"); }'),
 'other' => 'no expression',
);

$jsonObjectWithExpression = Zend_Json::encode($data,false,
     array('enableJsonExprFinder' => true)
  );

1 Comment

It useful if you using AJAX which gets json encoded new data with objects :)
1

The best way todo this in my opinion is to assign one controller as your json output, then you can do this:

class Api_IndexController extends Zend_Controller_Action {


    public function init() {
        $this->data = array();
    }


    public function preDispatch() {
        $this->variables = $this->_getAllParams();
    }


    public function postDispatch() {
        $this->_helper->json($this->data);
    }


    public function __call($name, $args) {
        return;
    }


    public function forumAction () {

        $this->mapper   = new ORM_Model_Mapper_Forum();
        $this->model    = new ORM_Model_Forum();
        $this->dbTable  = new ORM_Model_DbTable_Forum();

        if (isset($this->variables['id']) && is_numeric($this->variables['id'])) {
            $output = $this->model->find($this->variables['id']);

            if ($output->id == null) {
                return $this->_setError(404);
            }
        } else {
            $output = $this->mapper->fetchAllToArray();
        }

        $this->data = $output;
    }


    private function _setError($code=500) {
        $this->data = array('error' => $code);
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.