Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7795339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:03:08+00:00 2026-06-01T23:03:08+00:00

I would like to check from inside a controller if it is a secured

  • 0

I would like to check from inside a controller if it is a secured page or not.
How to do this ?

My use case is the following :

  • User can register and log in
  • If he logs in and tries to access a secured page, he will be redirected to a “beta version” page until the end of June.
  • If he tries to access a normal page (not secured), he will be able to access it without any redirection.

Thanks for your help !

Aurel

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T23:03:09+00:00Added an answer on June 1, 2026 at 11:03 pm

    When Symfony2 processes a request it matches the url pattern with each firewall defined in app/config/security.yml. When url pattern matches with a pattern of the firewall Symfony2 creates some listener objects and call handle method of those objects. If any listener returns a Response object then the loop breaks and Symfony2 outputs the response. Authentication part is done in authentication listeners. They are created from config defined in matched firewall e.g form_login, http_basic etc. If user is not authenticated then authenticated listeners create a RedirectResponse object to redirect user to login page. For your case, you can cheat by creating a custom authentication listener and add it in your secured page firewall. Sample implementation would be following,

    Create a Token class,

    namespace Your\Namespace;
    
    use Symfony\Component\Security\Core\Authentication\Token\AbstractToken;
    
    class MyToken extends AbstractToken
    {
        public function __construct(array $roles = array())
        {
            parent::__construct($roles);
        }
    
        public function getCredentials()
        {
            return '';
        }
    }
    

    Create a class that implements AuthenticationProviderInterface. For form_login listener it authenticates with the given UserProvider. In this case it will do nothing.

    namespace Your\Namespace;
    
    use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
    use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface;
    use Acme\BaseBundle\Firewall\MyToken;
    
    class MyAuthProvider implements AuthenticationProviderInterface
    {
    
        public function authenticate(TokenInterface $token)
        {
            if (!$this->supports($token)) {
                return null;
            }
    
            throw new \Exception('you should not get here');
        }
    
        public function supports(TokenInterface $token)
        {
            return $token instanceof MyToken;
        }
    

    Create an entry point class. The listener will create a RedirectResponse from this class.

    namespace Your\Namespace;
    
    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\HttpFoundation\Response;
    use Symfony\Component\Security\Core\Exception\AuthenticationException;
    use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
    use Symfony\Component\Security\Http\HttpUtils;
    
    
    class MyAuthenticationEntryPoint implements AuthenticationEntryPointInterface
    {
        private $httpUtils;
        private $redirectPath;
    
        public function __construct(HttpUtils $httpUtils, $redirectPath)
        {
            $this->httpUtils = $httpUtils;
            $this->redirectPath = $redirectPath;
        }
    
        /**
         * {@inheritdoc}
         */
        public function start(Request $request, AuthenticationException $authException = null)
        {
            //redirect action goes here
            return $this->httpUtils->createRedirectResponse($request, $this->redirectPath);
        }
    

    Create a listener class. Here you will implement your redirection logic.

    namespace Your\Namespace;
    
    use Symfony\Component\Security\Http\Firewall\ListenerInterface;
    use Symfony\Component\HttpKernel\Event\GetResponseEvent;
    use Symfony\Component\Security\Core\SecurityContextInterface;
    use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
    
    class MyAuthenticationListener implements ListenerInterface
    {
        private $securityContext;
        private $authenticationEntryPoint;
    
    
        public function __construct(SecurityContextInterface $securityContext, AuthenticationEntryPointInterface $authenticationEntryPoint)
        {
            $this->securityContext = $securityContext;
            $this->authenticationEntryPoint = $authenticationEntryPoint;
        }
    
        public function handle(GetResponseEvent $event)
        {
            $token = $this->securityContext->getToken();
            $request = $event->getRequest();
            if($token === null){
                return;
            }
    
            //add your logic
            $redirect = // boolean value based on your logic
    
            if($token->isAuthenticated() && $redirect){
    
                $response = $this->authenticationEntryPoint->start($request);
                $event->setResponse($response);
                return;
            }
        }
    
    }
    

    Create the services.

    <?xml version="1.0" ?>
    <container xmlns="http://symfony.com/schema/dic/services"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
    
        <services>
    
            <service id="my_firewall.security.authentication.listener"
                     class="Your\Namespace\MyAuthenticationListener"
                     parent="security.authentication.listener.abstract"
                     abstract="true">
                <argument type="service" id="security.context" />
                <argument /> <!-- Entry Point -->
            </service>
    
            <service id="my_firewall.entry_point" class="Your\Namespace\MyAuthenticationEntryPoint" public="false" ></service>
    
            <service id="my_firewall.auth_provider" class="Your\Namespace\MyAuthProvider" public="false"></service>
        </services>
    
    </container>
    

    Register the listener. Create a folder named Security/Factory in your bundles DependencyInjection folder. Then create the factory class.

    namespace Your\Bundle\DependencyInjection\Security\Factory;
    
    use Symfony\Component\DependencyInjection\ContainerBuilder;
    use Symfony\Component\DependencyInjection\Reference;
    use Symfony\Component\DependencyInjection\DefinitionDecorator;
    use Symfony\Component\DependencyInjection\Definition;
    use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\SecurityFactoryInterface;
    use Symfony\Component\Config\Definition\Builder\NodeDefinition;
    
    class MyFirewallFactory implements SecurityFactoryInterface
    {
    
        public function create(ContainerBuilder $container, $id, $config, $userProvider, $defaultEntryPoint)
        {
            $provider = 'my_firewall.auth_provider.'.$id;
            $container->setDefinition($provider, new DefinitionDecorator('my_firewall.auth_provider'));
    
            // entry point
            $entryPointId = $this->createEntryPoint($container, $id, $config, $defaultEntryPoint);
    
            // listener
            $listenerId = 'my_firewall.security.authentication.listener'.$id;
            $listener = $container->setDefinition($listenerId, new DefinitionDecorator('my_firewall.security.authentication.listener'));
            $listener->replaceArgument(1, new Reference($entryPointId));
            return array($provider, $listenerId, $entryPointId);
        }
    
        public function getPosition()
        {
            return 'pre_auth';
        }
    
        public function getKey()
        {
            return 'my_firewall'; //the listener name
        }
    
        protected function getListenerId()
        {
            return 'my_firewall.security.authentication.listener';
        }
    
        public function addConfiguration(NodeDefinition $node)
        {
            $node
                ->children()
                    ->scalarNode('redirect_path')->end()
                ->end()
                ;
        }
    
        protected function createEntryPoint($container, $id, $config, $defaultEntryPointId)
        {
            $entryPointId = 'my_firewall.entry_point'.$id;
            $container
                ->setDefinition($entryPointId, new DefinitionDecorator('my_firewall.entry_point'))
                ->addArgument(new Reference('security.http_utils'))
                ->addArgument($config['redirect_path'])
                ;
            return $entryPointId;
        }
    
    }
    

    Then in your NamespaceBundle.php of your bundle folder add the following code.

    public function build(ContainerBuilder $builder){
        parent::build($builder);
        $extension = $builder->getExtension('security');
        $extension->addSecurityListenerFactory(new Security\Factory\MyFirewallFactory());
    }
    

    Authentication listener is created, phew :). Now in your app/config/security.yml do following.

    api_area:
      pattern: ^/secured/
      provider: fos_userbundle
      form_login:
        check_path: /login_check
        login_path: /login
        csrf_provider: form.csrf_provider
      my_firewall:
        redirect_path: /beta
      logout: true
      anonymous: true
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to check what colors is present in a image. This will
I would like to check the load speed of each page in a particular
From inside a batch file, I would like to test whether I'm running with
I have a validation class which I would like to use to check all
I would like to finish an activity from inside the onCreate method. When I
I would like to check if a few URL's exst on my old website
I would like to check whether the request is XML od HTML. When HTML
I would like to check if an object is defined or exists using C#.
I would like to check the memory consumption of each running processes individually, cat
We would like to check if a specified process is currently running via PHP.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.