我打算在不使用eZ Publish呈现视图的情况下获得json响应。
所以我尝试使用自定义模块来执行此操作:
function jsonConvert(){
$articles = eZFunctionHandler::execute(
'content',
'tree',
array(
'parent_node_id' => '59'
)
);
header("Content-Type: application/json");
return json_encode($articles);
}
echo jsonConvert();
如何在不使用呈现domain.com/module/view/之类视图的基本URL的情况下编译此模块,以便在没有任何HTML代码的情况下获得json响应?
答案 0 :(得分:2)
echo json_encode( YOUR ARRAY );
eZExecution::cleanExit();
您需要在自定义模块/视图php文件中返回json所需的全部内容。
答案 1 :(得分:1)
如果我是你:
使用内置功能,允许您使用不同的布局来呈现内容视图。在layout.ini.append.php覆盖中创建一个新的布局'MYLAYOUT'(参见https://doc.ez.no/eZ-Publish/Technical-manual/4.x/Reference/Configuration-files/layout.ini),然后使用/ layout / set / MYLAYOUT / your / content / uri
在布局配置中指定内容类型以符合您的要求(application / json作为内容类型)
创建一个由新创建的布局使用的pagelayout.tpl模板,该模板基本上只包含{$ module_result.content}
创建一个模板运算符,将您的内容转换为'可读'的json,并从模板中调用它来呈现您的内容(可能是/node/view/full.tpl覆盖)
#4 =>替代(但不是那么性感)通过允许在模板中调用php函数,直接在模板中调用json_encode(参见https://doc.ez.no/eZ-Publish/Technical-manual/4.x/Reference/Configuration-files/template.ini/PHP/PHPOperatorList)
答案 2 :(得分:1)
要在模块中获取空白页面布局并设置json内容类型,可以在模块php文件中添加以下行:
header("Content-Type: application/json");
$Result = array();
$Result['content'] = json_encode($articles);
$Result['pagelayout'] = false;