gpt4 book ai didi

php - Slim.php `halt` 方法的行为与记录的不同?

转载 作者:可可西里 更新时间:2023-11-01 16:56:23 30 4
gpt4 key购买 nike

Slim 的文档阅读了以下关于框架的halt 方法的内容:

Halt

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.

例如

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

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

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

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

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

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?

最佳答案

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

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

关于php - Slim.php `halt` 方法的行为与记录的不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19654370/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com