-
-
Save zdenekdrahos/3cf45fe60e333ac6d524e36b4ce2883b to your computer and use it in GitHub Desktop.
Redirect authenticated user on anonymous pages in Symfony - wrong solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// app/AppKernel.php | |
class AppKernel extends Kernel | |
{ | |
public function registerBundles() | |
{ | |
$bundles = array( | |
// ... | |
new App\MyUserBundle(), | |
); | |
// ... | |
} | |
// ... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// src/MyUserBundle/Controller/LoggedUserRedirection.php | |
namespace App\MyUserBundle\Controller; | |
use Symfony\Component\HttpFoundation\RedirectResponse; | |
use Symfony\Component\Routing\RouterInterface; | |
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; | |
trait LoggedUserRedirection | |
{ | |
protected function redirectInController($container) | |
{ | |
return $this->redirectLoggedUser( | |
$container->get('security.authorization_checker'), | |
$container->get('router') | |
); | |
} | |
protected function redirectLoggedUser(AuthorizationCheckerInterface $security, RouterInterface $router) | |
{ | |
if ($security->isGranted('IS_AUTHENTICATED_FULLY')) { | |
$url = $router->generate('homepage'); | |
return new RedirectResponse($url); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// src/MyUserBundle/MyUserBundle.php | |
namespace App\MyUserBundle; | |
use Symfony\Component\HttpKernel\Bundle\Bundle; | |
class MyUserBundle extends Bundle | |
{ | |
public function getParent() | |
{ | |
return 'FOSUserBundle'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// src/MyUserBundle/DependencyInjection/MyUserExtension.php | |
namespace App\MyUserBundle\DependencyInjection; | |
use Symfony\Component\DependencyInjection\ContainerBuilder; | |
use Symfony\Component\HttpKernel\DependencyInjection\Extension; | |
class MyUserExtension extends Extension | |
{ | |
/** @SuppressWarnings(PHPMD.UnusedFormalParameter) */ | |
public function load(array $configs, ContainerBuilder $container) | |
{ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// src/AppBundle/Controller/RegistrationController.php | |
namespace AppBundle\Controller; | |
class RegistrationController extends BaseController | |
{ | |
use \App\MyUserBundle\Controllerr\LoggedUserRedirection; | |
/** | |
* @SFW\Route("registration") | |
* @SFW\Template() | |
*/ | |
public function registrationAction(Request $request) | |
{ | |
$response = $this->redirectInController($this->container); | |
if ($response) { | |
return $response; | |
} | |
// default action | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// src/MyUserBundle/Controller/ResettingController.php | |
namespace App\MyUserBundle\Controller; | |
use FOS\UserBundle\Controller\ResettingController as FosController; | |
class ResettingController extends FosController | |
{ | |
use LoggedUserRedirection; | |
public function requestAction() | |
{ | |
$response = $this->redirectInController($this->container); | |
if ($response) { | |
return $response; | |
} | |
return parent::requestAction(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// src/MyUserBundle/Controller/SecurityController.php | |
namespace App\MyUserBundle\Controller; | |
use Symfony\Component\HttpFoundation\Request; | |
use FOS\UserBundle\Controller\SecurityController as FosController; | |
class SecurityController extends FosController | |
{ | |
use LoggedUserRedirection; | |
public function loginAction(Request $request) | |
{ | |
$response = $this->redirectInController($this->container); | |
if ($response) { | |
return $response; | |
} | |
return parent::loginAction($request); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment