donglinxin8765 2013-10-29 09:33
浏览 39

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

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条回答 默认 最新

  • drep94225 2013-11-28 22:05
    关注

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

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?