我使用Phalcon Micro Application编写了一个网站。
在我的localhost上部署Web时,它可以使用所有HTTP方法正常工作。
但是,在将我的网络部署到真实主机时,即公共访问。我遇到一个问题,当调用api PUT时,服务器返回404未找到。 以下是我的代码:
$app->put('/api/news/{id}', function ($id) use ($app) {
$new = $app->request->getJsonRawBody();
$phql = "UPDATE News SET title = :title:, img = :img:, htmlContent = :htmlContent:, tag = :tag:, area = :area:, address = :address:, city=:city:, district = :district:, province = :province:, street = :street:, ward = :ward:, modifiedDate = :modifiedDate: WHERE id = :id:";
$status = $app->modelsManager->executeQuery($phql, array(
'id' => $id,
'title' => $new->title,
'img' => $new->img,
'htmlContent' => $new->htmlContent,
'area' => $new->area,
'address' => $new->address,
'city' => $new->city,
'district' => $new->district,
'province' => $new->province,
'street' => $new->street,
'ward' => $new->ward,
'tag' => $new->tag,
'modifiedDate' => date('Y-m-d H:i:s')
));
// Create a response
$response = new Response();
// Check if the insertion was successful
if ($status->success() == true) {
$response->setJsonContent(
array(
'status' => 'OK'
)
);
} else {
// Change the HTTP status
$response->setStatusCode(409, "Conflict");
$errors = array();
foreach ($status->getMessages() as $message) {
$errors[] = $message->getMessage();
}
$response->setJsonContent(
array(
'status' => 'ERROR',
'messages' => $errors
)
);
}
return $response;
});
我想这是一个跨域问题,然后我尝试在.htaccess上使用此设置,但它没有帮助。
AddDefaultCharset UTF-8
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
</IfModule>
# Always set these headers.
Header always set Access-Control-Allow-Origin "*"
Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT"
Header always set Access-Control-Max-Age "1000"
Header always set Access-Control-Allow-Headers "x-requested-with, Content-Type, origin, authorization, accept, client-security-token"
# Added a rewrite to respond with a 200 SUCCESS on every OPTIONS request.
RewriteEngine On
RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule ^(.*)$ $1 [R=200,L]
感谢您的任何帮助。