<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Validator\Constraints\Date;
class MainController extends AbstractController
{
/**
* @Route("/", name="index")
*/
public function index(): Response
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
return $this->render('index.html.twig', [
]);
}
//
// /**
// * @Route("/administration", name="admin")
// */
// public function admin(): Response
// {
// return $this->render('url/index.html.twig', [
// 'controller_name' => 'MainController',
// ]);
// }
/**
* @Route("/{urlShortcut}", name="urlRedirect", requirements={"urlShortcut"="[a-z,A-Z,0-9]{4,4}"})
*
*
*/
public function urlredirect($urlShortcut): Response
{
$em = $this->getDoctrine()->getManager();
$shortcut = $em->getRepository("App:UrlShortener")->findBy(['shortcut'=> $urlShortcut]);
if ($shortcut != null){
if (sizeof($shortcut) == 1){
$date = new \DateTime();
$shortcut[0]->setLastUsed($date);
$shortcut[0]->setNbUsed($shortcut[0]->getNbUsed() + 1);
$em->persist($shortcut[0]);
$em->flush();
return $this->redirect($shortcut[0]->getUrl());
}
}
return $this->render('url/404.html.twig');
}
/**
* @Route("/link/{urlShortcut}", name="long_urlRedirect")
*
*
*/
public function urlredirect_long($urlShortcut): Response
{
$urlShortcut = "link/".$urlShortcut;
$em = $this->getDoctrine()->getManager();
$shortcut = $em->getRepository("App:UrlShortener")->findBy(['shortcut'=> $urlShortcut]);
if ($shortcut != null){
if (sizeof($shortcut) == 1){
$date = new \DateTime();
$shortcut[0]->setLastUsed($date);
$shortcut[0]->setNbUsed($shortcut[0]->getNbUsed() + 1);
$em->persist($shortcut[0]);
$em->flush();
return $this->redirect($shortcut[0]->getUrl());
}
}
return $this->render('url/404.html.twig');
}
}