SLIM framework Halt call

送分小仙女□ 提交于 2019-12-11 16:47:05

问题


Have a question on Slim Framework php.

In my application, I would like to stop the application execution if a condition is mismatched.

There is a halt function, per Slim documentation. But that does not appear to be working. the application continuous to execute even after calling Halt.

pseudo code:

if ( $valid ) {
         // Do something
} else {
    $app->halt(500, "not valid");
}
// Other code here.


$app->run();

I was expecting that, we we call Halt function, the "Other code" should not execute. But it does not appear to be the case.

Any ideas?


回答1:


You can always call exit manually to stop the script execution.

$app->halt(500, "not valid");
exit;



回答2:


Halt should work. As Josh mentioned, it does need to be called from within a route callback. For example, I use the following as a custom 404 (inside of index.php) in the event that a specified route is not found.

    // not found (custom 404)
    $app->notFound(function () use ($app) {

            // build response
            $response = array(
                'type' => 'not_found',
                'message' => 'The requested resource does not exist.'
            );

            // output response
            $app->halt(404, json_encode($response));

    });

Note the reference to use ($app) -- more on this can be found here: http://www.slimframework.com/documentation/stable#scope-resolution




回答3:


Halt should only be invoked within the context of a route callback. I recommend you ask any further questions on the official Slim Framework support forum:

http://help.slimframework.com

Best regards, Josh




回答4:


You can use return if you are in a callback function

return $app->halt(500,"message");

or

$app->stop();

after halt function.



来源:https://stackoverflow.com/questions/10198085/slim-framework-halt-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!