src/Controller/HomeController.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Attribute\Route;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\RedirectResponse;
  9. use App\Repository\ShortUrlsRepository;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use App\Entity\ShortUrls;
  12. use App\Service\CreateVisitorsLog;
  13. class HomeController extends AbstractController
  14. {
  15.     
  16.     public function index(): Response
  17.     {
  18.         return $this->render('home/index.html.twig');
  19.     }
  20.     /**
  21.      * the method that forwards the short url to the actual url
  22.      * */
  23.     public function url_forward($codeShortUrlsRepository $repositoryCreateVisitorsLog $log_helper): Response
  24.     {
  25.         $short_code $code;
  26.         $url_details $repository->findOneBy(['short_code' => $short_code]);
  27.         if ($url_details === null) {
  28.             throw $this->createNotFoundException('Url not found');
  29.         }
  30.         else {
  31.             //record visits
  32.             //$log_helper->create_log();
  33.             //redirect to long url
  34.             $url $url_details->getUrl();
  35.             return new RedirectResponse($url);
  36.         }
  37.     }
  38. }