I am working on a Symphony app and have tried to create a route using PHP. The problem is that even though the route is correctly defined in the backend, I get a 404 status code in the frontend when I try to retrieve the route.
Backend:
/**
* @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);
}
And this is my front-end code:
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:");
});
Does anyone have any idea why I get the 404 error when I call the route?
Maybe you have to clear the php-cache.
php bin/console cache:clear
execute the command in prod folder