SLIM框架暂停调用

Kev*_*ave 0 php frameworks halt execution slim

对Slim Framework php有疑问.

在我的应用程序中,如果条件不匹配,我想停止应用程序执行.

根据Slim文档,有一个暂停功能.但这似乎并没有起作用.即使在调用Halt之后,应用程序也会连续执行.

伪代码:

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


$app->run();
Run Code Online (Sandbox Code Playgroud)

我期待的是,我们称之为Halt函数,"其他代码"不应该执行.但似乎并非如此.

有任何想法吗?

Nic*_*ons 6

停止应该工作.正如Josh所说,它确实需要在路由回调中调用.例如,如果找不到指定的路由,我将使用以下内容作为自定义404(在index.php中).

    // 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));

    });
Run Code Online (Sandbox Code Playgroud)

请注意使用的参考($ app) - 有关详细信息,请访问:http://www.slimframework.com/documentation/stable#scope-resolution