【问题标题】:Slim PHP Not returning the correct Status CodeSlim PHP 没有返回正确的状态码
【发布时间】:2016-09-09 19:37:20
【问题描述】:

我正在创建我的第一个 api,我选择使用 Slim PHP,到目前为止,我认为它是一个很棒的轻量级框架,可以满足我的基本需求。我真正遇到的唯一问题是我的路由响应没有返回正确的状态代码。我想在成功登录时返回 200,在登录失败且凭据不正确时返回 403。我得到的只是200,不管它返回什么。逻辑是有效的,因为我可以看到返回了正确的 JSON,只是状态码没有改变。

索引.php

 <?php
    use \Psr\Http\Message\ServerRequestInterface as Request;
    use \Psr\Http\Message\ResponseInterface as Response;

    require  'vendor/autoload.php';

    $app = new \Slim\App;

    $app->options('/{routes:.+}', function ($request, $response, $args) {
        return $response;
    });

    $app->add(function ($req, $res, $next) {
        $response = $next($req, $res);
        return $response
            ->withHeader('Access-Control-Allow-Origin', 'http://mysite')
            ->withHeader('Access-Control-Allow-Headers', 'X-Requested-With,    Content-Type, Accept, Origin, Authorization')
            ->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
    });

    require_once 'api/login.php';

    $app->run();

登录.php

$app->post('/api/login', function ($request, $response){
    require_once 'db.php';

    $q = "SELECT * FROM blog_admin WHERE username = ? AND password = ?";

    $stmt = $mysqli->prepare($q);
    $stmt->bind_param('ss', $user, $pass);

    $user = $request->getParsedBody()['username'];
    $pass = md5($request->getParsedBody()['password']);

    if($stmt->execute()){
        $stmt->store_result();

        if($stmt->num_rows > 0){
            $token = md5(uniqid($user, true));
            date_default_timezone_set("America/Toronto");
            $logged_in = date('Y/m/d');
            $data = array(
                "flash" => 'success',
                "message" => '<strong>SUCCESS:</strong> You have entered the correct login information! Please wait while you are redirected...',
                '_token' => $token,
                'logged_in' => $logged_in
            );

            $q = "UPDATE blog_admin SET _token=?, last_logged_in=?";
            $stmt = $mysqli->prepare($q);
            $stmt->bind_param('ss', $token, $logged_in);

            if($stmt->execute()){
                $response->withJson($data, 200);
            }else{
                $data = array(
                    "flash" => 'danger',
                    "message" => '<strong>ERROR:</strong> Could not login! Please try again later!'
                );
                $response->withJson($data, 403);
            }
        }else{
            $data = array(
                "flash" => 'danger',
                "message" => '<strong>ERROR:</strong> The Username/Password you have entered is incorrect. Please try again.'
            );
            $response->withJson($data, 403);
        }
    }else{
        $data = array(
            "flash" => 'danger',
            "message" => '<strong>ERROR:</strong> Could Not Run the SQL'
        );

        $response->withJson($data, 500);
    }

    return $response;
});

我不确定问题可能是什么,所以任何想法都将不胜感激。

【问题讨论】:

    标签: php slim


    【解决方案1】:

    slim3 使用的 PSR-7 响应是 immutable value object,因此无法更改。

    F.ex.

    $response->withJson($data, 200);
    

    不会更改 $response 它会返回更改后的响应,因此您要么必须返回此

    return $response->withJson($data, 200);
    

    或者你需要用新值重新分配变量,然后在路由函数的末尾返回它。

    $response = $response->withJson($data, 200);
    // other code
    return $response;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-19
      • 2017-07-29
      • 1970-01-01
      • 2015-07-26
      • 2013-09-22
      • 1970-01-01
      相关资源
      最近更新 更多