我在 PHP Micro Slim Framework @version 2.3.5 中遇到有关页面重定向的问题。 我已经尝试了几种方法来重定向,但我尝试的所有方式都不起作用。
实际上我有一个登录页面并且在提交时我正在调用我已经写过的登录检查功能并且它正常工作。但最终在用户名和密码成功匹配后,我想重定向另一页。
我已经尝试过的方法如下:
return $response->withRedirect('/dashboard.php');
return $response->withStatus(302)->withHeader('Location', 'http://localhost/kafcashapp/dashboard.php');
$app->redirect('http://localhost/kafcashapp/dashboard.php', 301);
header("Location: http://localhost/kafcashapp/dashboard.php");
以下是检查登录的代码
$app->post('/login', function() use ($app) {
// check for required params
verifyRequiredParams(array('email', 'password'));
// reading post params
$email = $app->request()->post('email');
$password = $app->request()->post('password');
$response = array();
$db = new DbHandler();
// check for correct email and password
if ($db->checkLogin($email, $password)) {
// get the user by email
$user = $db->getUserByEmail($email);
if ($user != NULL) {
$response["error"] = false;
$response['name'] = $user['name'];
$response['email'] = $user['email'];
$response['apiKey'] = $user['api_key'];
$response['createdAt'] = $user['created_at'];
} else {
// unknown error occurred
$response['error'] = true;
$response['message'] = "An error occurred. Please try again";
}
} else {
// user credentials are wrong
$response['error'] = true;
$response['message'] = 'Login failed. Incorrect credentials';
}
//echoRespnse(200, $response);
});
任何建议都将不胜感激。
答案 0 :(得分:0)
使用以下内容查看documentation您可以302 Temporary Redirect
。
$app->redirect("/bar");
如果您需要301 Moved Permanently
,则需要手动指定状态代码。
$app->redirect("/bar", 301);