Slim.php`halt`方法的行为与记录不同? [英] Slim.php `halt` method behaves differently than documented?

查看:145
本文介绍了Slim.php`halt`方法的行为与记录不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Slim的文档在框架的 halt 方法中读取以下内容:

Slim's documentation reads the following in regard to the framework's halt method:


暂停

Halt

Slim应用程序的halt()方法将立即返回带有给定状态代码和正文的
HTTP响应。此方法接受
两个参数:HTTP状态代码和可选消息。 Slim将
立即暂停当前应用程序,并将HTTP响应发送到具有指定状态和可选消息的客户端
(作为
响应正文)。这将覆盖现有的\Slim\Http \Response
对象。

The Slim application’s halt() method will immediately return an HTTP response with a given status code and body. This method accepts two arguments: the HTTP status code and an optional message. Slim will immediately halt the current application and send an HTTP response to the client with the specified status and optional message (as the response body). This will override the existing \Slim\Http\Response object.

例如。


e.g.

//Send a default 500 error response
$app->halt(500);

//Or if you encounter a Balrog...
$app->halt(403, 'You shall not pass!');

因为我在这方面遇到了一些问题,所以我已经构建了以下测试应用程序,他们的文档:

Because I'm having some problems in this area, I've built the following test application, according to their documentation:

<?php

// Include Slim framework dependencies;
require '../lib/Slim/Slim.php';

\Slim\Slim::registerAutoloader(); // Slim's autloader;
$app = new \Slim\Slim();

//Send a default 500 error response
$app->halt(500);

有趣的是,我得到的回应是:

And interestingly, the response I get is:

Fatal error: Uncaught exception 'Slim\Exception\Stop' in D:\projects\myApplication\api\lib\Slim\Slim.php:1004 Stack trace: #0 D:\projects\myApplication\api\lib\Slim\Slim.php(1024): Slim\Slim->stop() #1 D:\projects\myApplication\api\app\app.php(10): Slim\Slim->halt(500) #2 D:\projects\myApplication\api\public\index.php(4): include_once('D:\__projects\S...') #3 {main} thrown in D:\projects\myApplication\api\lib\Slim\Slim.php on line 1004

不用说,HTTP响应代码是200.那么Slim究竟发生了什么?为什么不是HTTP响应代码500?

Needless the say, the HTTP response code is 200. So what's going on with Slim, exactly? Why isn't the HTTP response code 500?

推荐答案

不允许在路由回调之外调用halt()方法。
你应该这样使用;

It is not allowed to call halt() method outside of the route callback. You should use like this;

$app->get('/method/', function () {
  //logical controls
  //do something
    //or
  $app->halt();
});   

这篇关于Slim.php`halt`方法的行为与记录不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆