Welcome Guest, Not a member yet? Register   Sign In
How to exit friendly in my controller
#1
Wink 

I wrote a global function below named outputJson in my project to print JSON and exit.

PHP Code:
/**
 * 格式化Json输出并终止程序
 */
function outputJson($status$msg=''$data=''){
    //header("content-type:application/json");
    \Config\Services::response()->setBody(json_encode(['code'=>$status'msg'=>$msg'data'=>$data]))->send();
    die();



I found a problem when I call PHP native functions "exit/quit" to terminate my program,The rest codes of CI4 framework after running the controller weren't be executed, Such as the After Filter mechanism, Send Response, and Release some instances.
I think calling the exit/quit function to terminate directly my program that is really more convenient than returning recursively in some cases.
Is there any way or more friendly function offered by CI4 Framework to replace exit/quit?
Thanks in advance.
Reply


Messages In This Thread
How to exit friendly in my controller - by yongplus - 11-22-2021, 07:55 PM



Theme © iAndrew 2016 - Forum software by © MyBB