我正在开发 Symphony 应用程序,并尝试使用 PHP 创建路线。问题是,即使在后端正确定义了路由,当我尝试检索路由时,我也会在前端收到 404 状态代码。
后端:
/**
* @Route("/fetch-stock-data")
*/
public function fetchStockDataAction(Request $request) {
$this->disableViewAutoRender();
session_start();
$currentTime = time();
$oneHour = 60 * 60; // 3600 seconds
if (!isset($_SESSION['stock_data_timestamp']) || $currentTime - $_SESSION['stock_data_timestamp'] > $oneHour) {
session_write_close();
$apiKey = "KEY";
$symbol = "AAPL";
$queryURL = "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol={$symbol}&outputsize=full&apikey={$apiKey}";
$response = file_get_contents($queryURL);
$data = json_decode($response, true);
session_start();
$_SESSION['stock_data'] = $data;
$_SESSION['stock_data_timestamp'] = $currentTime;
} else {
$data = $_SESSION['stock_data'];
session_write_close();
}
return new JsonResponse($data);
}
这是我的前端代码:
var apiUrl = `/fetch-stock-data`;
$.get(apiUrl, null, function (data) {
globalStockData = data["Time Series (Daily)"];
document.getElementById('cookieConsentMessage').style.display = "none";
updateChart();
}).fail(function() {
console.error("Error with response:");
});
有人知道为什么我在调用路线时收到 404 错误吗?
也许你必须清除 php 缓存。
php bin/console cache:clear
执行prod文件夹中的命令