Slim.php `halt` method behaves differently than documented?

ぃ、小莉子 提交于 2019-12-24 14:27:51

问题


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

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.

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

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?


回答1:


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();
});   


来源:https://stackoverflow.com/questions/19654370/slim-php-halt-method-behaves-differently-than-documented

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