<?php
/**
* Copyright (c) 2022 by Noma Solutions.
* This software is the proprietary information of Noma Solutions.
*
* All rights reserved
*/
namespace BackendApi\Application\Controller\App;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class FrontendController extends AbstractController
{
private bool $frontendEnabled;
private string $projectPath;
public function __construct(bool $frontendEnabled, string $projectPath)
{
$this->frontendEnabled = $frontendEnabled;
$this->projectPath = $projectPath;
}
/**
* @Route("/", name="homepage")
*/
public function index(): RedirectResponse
{
return $this->redirectToRoute('app_admin');
}
/**
* @Route("/front")
* @Route("/front/{moduleUrl}", name="angular_index", requirements={"moduleUrl": "^(?!(test|doc|api|_wdt|_profiler|admin|coreapi|oauth|webview|app\/doc\/|app\/doc$)).*"})
* @param Request $request
* @return Response
*/
public function indexAction(Request $request): Response
{
if (!$this->frontendEnabled) {
throw $this->createNotFoundException();
}
$indexHtmlPath = $this->projectPath . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'index.html';
if (!file_exists($indexHtmlPath)) {
throw $this->createNotFoundException('No index.html file in public dir');
}
$contents = file_get_contents($indexHtmlPath);
$contents = str_replace([
'http://localhost:10010'
], [
$request->getSchemeAndHttpHost()
], $contents);
return new Response($contents);
}
}