duanhan5388 2015-03-23 15:57
浏览 25

CakePHP - JsonView渲染是控制器的子功能

I'm using Cakephp with json parse extension and the RequestHandler component in order to create Web services using json.

I created a controller named Ws In this controller I have a named userSubscribe

In order to avoid a lot of If else statements in the next methods, I thought about using a private function inside this controller that will check somes conditions and stop the script normaly BUT ALSO render the json normaly. I just want to do a DRY way !

My question is :

How could I render the json view in a sub function (called by the userSubscribe) ?

To make it clear, here is the style code that would like

public function userSubscribe() {
    $this->check();

    // Following code only executed if check didn't render the json view
    // $data = ...
    $code = 1;
    $i = 2;
}

private function check() {
    $input = &$this->request->data;
    if ($_SERVER["CONTENT_TYPE"] != "application/json") { // For example
        $result = "KO";
        $this->set(compact("result"));
        $this->set('_serialize', 'result');

        $this->render(); // HERE, it will stop the 'normal behaviour' and render the json with _serialize
    }
    if (!isset($input["input"])) {
        $result = "KO";
        $this->set(compact("result"));
        $this->set('_serialize', 'result');

        $this->render(); // HERE, it will stop the 'normal behaviour' and render the json with _serialize
    }
}

It's seems to be quite simple to do, but why can't I find the answer ?!

Thanks in advance for clue/advise/anything !

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 C++ yoloV5改写遇到的问题
    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
    • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
    • ¥15 帮我写一个c++工程
    • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
    • ¥15 关于smbclient 库的使用
    • ¥15 微信小程序协议怎么写
    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?