title: Security and routes - Symfony2
body: <p>I want the index page for my project to be a login form with a link for registration below it and unlogged visitors should be able to see only the login form with route <code>/</code> and the register page with route <code>/register</code>. When the log I want they to be redirected to the home page with route <code>/home</code>. I tried some things and it's working in the dev environment (although having some troubles with the toolbar - <a href="https://stackoverflow.com/questions/12073945/symfony2-dev-environment">Symfony2 - dev environment</a>) but when I switch to prod env, the browser says: "The page isn't redirecting properly. Firefox has detected that the server is redirecting the request for this address in a way that will never complete. This problem can sometimes be caused by disabling or refusing to accept cookies." </p>
  
  <p>Here are my files:</p>
  
  <p><strong>security.yml</strong></p>
  
  <pre><code>security:
      encoders:
          EM\MyFriendsBundle\Entity\User:
              algorithm:        sha1
              encode_as_base64: false
              iterations:       1
  
  role_hierarchy:
      ROLE_ADMIN:       ROLE_USER
  
  providers:
      administrators:
          entity: { class: EMMyFriendsBundle:User }
  
  firewalls:
      secured_area:
          pattern:    ^/
          anonymous: ~
          form_login:
              login_path:  /login
              check_path:  /login_check
              default_target_path: /home
  
  access_control:
      - { path: ^/home, roles: ROLE_ADMIN }
  </code></pre>
  
  <p><strong>routing.yml</strong></p>
  
  <pre><code>login_display:
      pattern: /
      defaults: { _controller: EMMyFriendsBundle:Welcome:display }
  
  login:
      pattern:   /login
      defaults:  { _controller: EMMyFriendsBundle:Welcome:login}
  
  login_check:
      pattern:   /login_check
  
  register:
      pattern: /register
      defaults: { _controller: EMMyFriendsBundle:Welcome:register }
  
  home_display:
      pattern: /home
      defaults: { _controller: EMMyFriendsBundle:Home:display }
  </code></pre>
  
  <p><strong>WelcomeController.php</strong></p>
  
  <pre><code>&lt;?php
  
  namespace EM\MyFriendsBundle\Controller;
  
  use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  use Symfony\Component\Security\Core\SecurityContext;
  
  class WelcomeController extends Controller
  {
      public function displayAction()
      {
          $error=null;
          $last_username=null;
          return $this-&gt;render('EMMyFriendsBundle:Welcome:login.html.twig', array('error' =&gt; $error, 'last_username' =&gt; $last_username));
      }
  
      public function loginAction()
      {
          $request = $this-&gt;getRequest();
          $session = $request-&gt;getSession();
  
          // get the login error if there is one
          if ($request-&gt;attributes-&gt;has(SecurityContext::AUTHENTICATION_ERROR)) {
              $error = $request-&gt;attributes-&gt;get(SecurityContext::AUTHENTICATION_ERROR);
          } else {
              $error = $session-&gt;get(SecurityContext::AUTHENTICATION_ERROR);
              $session-&gt;remove(SecurityContext::AUTHENTICATION_ERROR);
          }
  
          return $this-&gt;render('EMMyFriendsBundle:Welcome:login.html.twig', array(
              // last username entered by the user
              'last_username' =&gt; $session-&gt;get(SecurityContext::LAST_USERNAME),
              'error'         =&gt; $error
          ));
      }
  
      public function registerAction()
      {
           return $this-&gt;render('EMMyFriendsBundle:Welcome:register.html.twig');
      }
  }
  </code></pre>
  
  <p><strong>HomeController.php</strong></p>
  
  <pre><code>&lt;?php
  namespace EM\MyFriendsBundle\Controller;
  
  use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  
  class HomeController extends Controller
  {
      public function displayAction()
      {
          return $this-&gt;render('EMMyFriendsBundle:Home:home.html.twig');
      }
  }
  ?&gt;
  </code></pre>