Slim-3 php框架显示页面未找到错误,即使网址正确也是如此。 我正在使用slim3框架在php中为android应用程序编写网络服务。 几天前它无法正常工作。相同的代码现在无法正常工作。它始终返回未找到的页面。我认为index.php文件中存在错误,但我无法弄清楚。
我已经尽力解决此问题。 我更改了目录结构和.htaccess文件,但仍未解决。我正在使用xampp服务器。
index.php
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require '../ChickenShop/vendor/autoload.php';
require_once '../ChickenShop/public/includes/DbOperations.php';
//Creating a new app with the config to show errors
$app = new \Slim\App([
'settings' => [
'displayErrorDetails' => true
]
]);
//registering a new user
$app->post('/register', function (Request $request, Response $response) {
if (isTheseParametersAvailable(array('name', 'email', 'password', 'phone','address'))) {
$requestData = $request->getParsedBody();
$name = $requestData['name'];
$email = $requestData['email'];
$password = $requestData['password'];
$phone = $requestData['phone'];
$address = $requestData['address'];
$db = new DbOperations();
$responseData = array();
$result = $db->registerUser($name, $email, $password, $phone, $address);
if ($result == USER_CREATED) {
$responseData['error'] = false;
$responseData['message'] = 'Registered successfully';
$responseData['user'] = $db->getUserByEmail($email);
} elseif ($result == USER_CREATION_FAILED) {
$responseData['error'] = true;
$responseData['message'] = 'Some error occurred';
} elseif ($result == USER_EXIST) {
$responseData['error'] = true;
$responseData['message'] = 'This email already exist, please login';
}
$response->getBody()->write(json_encode($responseData));
}
});
//user login route
$app->post('/login', function (Request $request, Response $response) {
if (isTheseParametersAvailable(array('email', 'password'))) {
$requestData = $request->getParsedBody();
$email = $requestData['email'];
$password = $requestData['password'];
$db = new DbOperations();
$responseData = array();
if ($db->userLogin($email, $password)) {
$responseData['error'] = false;
$responseData['user'] = $db->getUserByEmail($email);
} else {
$responseData['error'] = true;
$responseData['message'] = 'Invalid email or password';
}
$response->getBody()->write(json_encode($responseData));
}
});
//getting all products
$app->get('/products', function (Request $request, Response $response) {
$db = new DbOperations();
$products = $db->getAllProducts();
$response->getBody()->write(json_encode(array("products" => $products)));
});
//function to check parameters
function isTheseParametersAvailable($required_fields)
{
$error = false;
$error_fields = "";
$request_params = $_REQUEST;
foreach ($required_fields as $field) {
if (!isset($request_params[$field]) || strlen(trim($request_params[$field])) <= 0) {
$error = true;
$error_fields .= $field . ', ';
}
}
if ($error) {
$response = array();
$response["error"] = true;
$response["message"] = 'Required field(s) ' . substr($error_fields, 0, -2) . ' is missing or empty';
echo json_encode($response);
return false;
}
return true;
}
// Run app
$app->run();
我尝试访问的URL:
http://localhost/ChickenShop/public/
Content of .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
我希望它能正常工作。