0

I use Zend Framework 2. I get data from server with AJAX and how can i return formatted data with JSON. (For example multi currency format as in the .phtml file $this->currencyFormat(1234.56, "TRY", "tr_TR")) I cannot use view helper from action.

My code like this. (MyController.php)

<?php
class MyController extends AbstractActionController
{
    public function myAction(){
        $data = array();
        //i want to format this data for multi currency format. as us,fr,tr etc..
        $data['amount'] = '100'; 

        return new JsonModel(data);
    }

}
Charles
  • 50,943
  • 13
  • 104
  • 142
Yargicx
  • 1,704
  • 3
  • 16
  • 36
  • Charles, this code works. I tested it. It returns the following valid JSON: `{"amount":"100"}`. Something else is the issue. I'll be happy to help with more information. Specifically, if you can find an error message or an unexpected behavior that you can relay, that would be helpful. – Ben Mar 21 '14 at 04:30
  • $data['amount'] = $this->currencyFormat(100.00, "TRY", "tr_TR")); doesn't work in my controller. – Yargicx Mar 23 '14 at 22:49
  • I have added an answer that formats currency in the controller. – Ben Mar 25 '14 at 13:05

2 Answers2

1

Yargicx, thanks for asking this question. It caused me to learn a new feature of PHP (5.3). Here is the code that returns the answer to your original question: formatted currency in JSON. It may look a little weird if you aren't familiar with invokable classes, but I'll explain how it works.

use Zend\I18n\View\Helper\CurrencyFormat;
use Zend\View\Model\JsonModel;

class MyController extends AbstractActionController
{
    public function myAction()
    {
        $data = array();
        //i want to format this data for multi currency format. as us,fr,tr etc..
        $currencyFormatter = new CurrencyFormat();
        $data['amount'] = $currencyFormatter(1234.56, "TRY", "tr_TR");

        return new JsonModel($data);
    }
}

To get to this point, I looked up the currencyFormat() method in the Zend\I18n\View\Helper\CurrencyFormat class and noticed that this was a protected method, thus I couldn't use it directly in the controller action.

I then noticed that there was a magic __invoke() method and (having never seen this before) I looked up the PHP documentation for it at http://www.php.net/manual/en/language.oop5.magic.php#object.invoke. As it turns out, you can use an object as if it is a function like the following. Note the very last line:

class Guy
{
    public function __invoke($a, $b)
    {
        return $a + $b;
    }
}

$guy = new Guy();
$result = $guy();

Since the __invoke() method of the Zend\I18n\View\Helper\CurrencyFormat class returns the result of a call to the currencyFormat() method, we can invoke the class with the same parameters as the currencyFormat() method, resulting in the original codeblock in this answer.

For kicks, here is the source code of the __invoke() function of the Zend\I18n\View\Helper\CurrencyFormat class:

public function __invoke(
    $number,
    $currencyCode = null,
    $showDecimals = null,
    $locale       = null,
    $pattern      = null
) {
    if (null === $locale) {
        $locale = $this->getLocale();
    }
    if (null === $currencyCode) {
        $currencyCode = $this->getCurrencyCode();
    }
    if (null === $showDecimals) {
        $showDecimals = $this->shouldShowDecimals();
    }
    if (null === $pattern) {
        $pattern = $this->getCurrencyPattern();
    }

    return $this->formatCurrency($number, $currencyCode, $showDecimals, $locale, $pattern);
}
Ben
  • 713
  • 4
  • 12
0

try

return new JsonModel('data' => $data);

everything else should work

Edit

$jsonencoded = \Zend\Json\Json::encode($data);//json string

$jsondecode = \Zend\Json\Json::decode($jsonencoded, \Zend\Json\Json::TYPE_ARRAY);

http://framework.zend.com/manual/2.0/en/modules/zend.json.objects.html

This is what you mean?

Skaza
  • 466
  • 4
  • 13
  • how can i format $data['amount'] = 100 as in a .phtml file. i want to use helper in the controller like this $data['amount']=$this->currencyFormat(100, "TRY", "tr_TR") – Yargicx Mar 20 '14 at 10:31
  • in .phtml file you can use $data['amount'] – Skaza Mar 20 '14 at 10:35
  • but i want to use it in controller not in .phtml file. i need view helper class in my controller for my json data. i want to return formatted json data. – Yargicx Mar 20 '14 at 13:07
  • $data['amount'] = 100; return new JsonModel('data' => $data); for example i want to return amount as france's currency format. how can i do this in a controller. not a in view, in a controller. – Yargicx Mar 20 '14 at 15:32
  • use json decode and encode not jsonmodel $jsonencoded = \Zend\Json\Json::encode($data);//json string $jsondecode = \Zend\Json\Json::decode($jsonencoded, \Zend\Json\Json::TYPE_ARRAY); – Skaza Mar 21 '14 at 06:59