Slim PHP不返回正确的状态代码

时间:2016-09-09 02:16:16

标签: php slim

我正在创建我的第一个api,我选择使用Slim PHP,到目前为止,我认为这是一个很好的轻量级框架,可以完成我需要的基础知识。我真正遇到的唯一问题是我的路线响应没有返回正确的状态代码。我想在成功登录时返回200,并在登录失败时使用不正确的凭据返回403。无论它返回什么,我所得到的只是200。逻辑正在运行,因为我可以看到返回正确的JSON,只是状态代码不会被更改。

的index.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();

的login.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;
});

我不确定问题是什么,所以任何想法都会受到高度赞赏。

1 个答案:

答案 0 :(得分:3)

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;