Slim PHP 没有返回正确的状态码
Slim PHP Not returning the correct Status Code
我正在创建我的第一个 api,我选择使用 Slim PHP,到目前为止,我认为它是一个很棒的轻量级框架,可以完成我需要的基础工作。我真正遇到的唯一问题是我的路由响应没有 return 正确的状态代码。我想在成功登录时返回 return 200,在凭据不正确的登录失败时返回 403。不管 returns 是什么,我得到的都是 200。逻辑是有效的,因为我可以看到正确的 JSON 被 returned,只是状态代码没有改变。
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;
});
我不确定可能是什么问题,所以任何想法将不胜感激。
slim3使用的PSR-7 Response是immutable value object,所以不能改
F.ex.
$response->withJson($data, 200);
不会更改 $response
它 return 更改后的响应 所以你要么必须 return 这个
return $response->withJson($data, 200);
或者您需要用新值重新分配变量,然后 return 它在路由函数的末尾。
$response = $response->withJson($data, 200);
// other code
return $response;
我正在创建我的第一个 api,我选择使用 Slim PHP,到目前为止,我认为它是一个很棒的轻量级框架,可以完成我需要的基础工作。我真正遇到的唯一问题是我的路由响应没有 return 正确的状态代码。我想在成功登录时返回 return 200,在凭据不正确的登录失败时返回 403。不管 returns 是什么,我得到的都是 200。逻辑是有效的,因为我可以看到正确的 JSON 被 returned,只是状态代码没有改变。
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;
});
我不确定可能是什么问题,所以任何想法将不胜感激。
slim3使用的PSR-7 Response是immutable value object,所以不能改
F.ex.
$response->withJson($data, 200);
不会更改 $response
它 return 更改后的响应 所以你要么必须 return 这个
return $response->withJson($data, 200);
或者您需要用新值重新分配变量,然后 return 它在路由函数的末尾。
$response = $response->withJson($data, 200);
// other code
return $response;