【问题标题】:Return string without using a template in Nette PHP framework在 Nette PHP 框架中不使用模板返回字符串
【发布时间】:2017-05-11 10:58:36
【问题描述】:

在不加载模板的情况下,在 nette php 框架中返回 json 字符串或屏幕上的简单文本的最佳方法是什么?

【问题讨论】:

    标签: php nette


    【解决方案1】:

    你可以在你的presenter中使用sendJson($data)方法。

    function renderDefault()
    {
        $data = ['hello' => 'world'];
        $this->sendJson($data);
    }
    

    将输出 {"hello":"world"}application/json Content-Type

    【讨论】:

    • 非常感谢 grez 什么是基本文本。 nette 框架并没有很好地解释这一点。
    • 如果您查看source codesendJson,您会发现它只适用于$this->sendResponse(new Responses\JsonResponse($data));,因此对于简单的文本,只需要另一种类型的响应 - TextResponse => $this->sendResponse(new TextResponse($data));
    猜你喜欢
    • 2023-02-22
    • 1970-01-01
    • 1970-01-01
    • 2020-02-17
    • 1970-01-01
    • 2018-10-09
    • 2019-07-11
    • 1970-01-01
    • 2015-09-18
    相关资源
    最近更新 更多