【问题标题】:ZF2 how to use View Helper for JsonModel Ajax CallZF2 如何使用 View Helper 进行 JsonModel Ajax 调用
【发布时间】:2014-03-19 21:21:12
【问题描述】:

我使用 Zend Framework 2。我使用 AJAX 从服务器获取数据,以及如何使用 JSON 返回格式化数据。 (例如 .phtml 文件中的多货币格式 $this->currencyFormat(1234.56, "TRY", "tr_TR")) 我不能从操作中使用视图助手。

我的代码是这样的。 (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);
    }

}

【问题讨论】:

  • 查尔斯,这段代码有效。我测试了它。它返回以下有效 JSON:{"amount":"100"}。还有别的问题。我很乐意为您提供更多信息。具体来说,如果您可以找到可以转发的错误消息或意外行为,那将很有帮助。
  • $data['amount'] = $this->currencyFormat(100.00, "TRY", "tr_TR"));在我的控制器中不起作用。
  • 我添加了一个在控制器中格式化货币的答案。

标签: php ajax zend-framework2 jsonmodel


【解决方案1】:

Yargicx,感谢您提出这个问题。它使我了解了 PHP (5.3) 的一个新特性。这是返回原始问题答案的代码:JSON 格式的货币。如果您不熟悉可调用类,它可能看起来有点奇怪,但我会解释它是如何工作的。

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);
    }
}

为了达到这一点,我在Zend\I18n\View\Helper\CurrencyFormat 类中查找了currencyFormat() 方法,发现这是一个受保护的方法,因此我不能直接在控制器操作中使用它。

然后我注意到有一个神奇的__invoke() 方法,并且(以前从未见过)我在http://www.php.net/manual/en/language.oop5.magic.php#object.invoke 查找了它的PHP 文档。事实证明,您可以像使用如下函数一样使用对象。注意最后一行:

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

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

由于Zend\I18n\View\Helper\CurrencyFormat类的__invoke()方法返回的是调用currencyFormat()方法的结果,所以我们可以调用与currencyFormat()方法相同参数的类,得到原始代码块在这个答案。

顺便说一句,这里是Zend\I18n\View\Helper\CurrencyFormat类的__invoke()函数的源代码:

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);
}

【讨论】:

    【解决方案2】:

    试试

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

    其他一切都应该工作

    编辑

    $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

    这就是你的意思?

    【讨论】:

    • 如何将 $data['amount'] = 100 格式化为 .phtml 文件。我想像这样在控制器中使用帮助器 $data['amount']=$this->currencyFormat(100, "TRY", "tr_TR")
    • 在 .phtml 文件中可以使用 $data['amount']
    • 但我想在控制器中使用它而不是在 .phtml 文件中。我需要在我的控制器中查看帮助程序类来获取我的 json 数据。我想返回格式化的 json 数据。
    • $data['amount'] = 100; return new JsonModel('data' => $data);例如,我想以法国货币格式返回金额。我怎么能在控制器中做到这一点。不在视图中,在控制器中。
    • 使用json decode 和encode not jsonmodel $jsonencoded = \Zend\Json\Json::encode($data);//json string $jsondecode = \Zend\Json\Json::decode($ jsonencoded, \Zend\Json\Json::TYPE_ARRAY);
    猜你喜欢
    • 1970-01-01
    • 2012-09-28
    • 1970-01-01
    • 1970-01-01
    • 2011-06-29
    • 1970-01-01
    • 1970-01-01
    • 2019-02-15
    • 1970-01-01
    相关资源
    最近更新 更多