src/Controller/DefaultController.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Component\Security\Core\Security;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. class DefaultController extends AbstractController
  9. {
  10.     private $security;
  11.     public function __construct(Security $security)
  12.     {
  13.         $this->security $security;
  14.     }
  15.     /**
  16.      * @Route("/", name="default")
  17.      */
  18.     public function index(): Response
  19.     {
  20.         $user $this->security->getUser();
  21.         if (!$user) {
  22.             return $this->redirectToRoute("app_login");
  23.         }
  24.         $roles $user->getRoles();
  25.         foreach (['admin''business''staff''customer'] as $role)
  26.             if (in_array('ROLE_' strtoupper($role), $roles)) {
  27.                 return $this->redirectToRoute($role);
  28.             }
  29.     }
  30. }