Use the same Preview for multiple forms

Asked

Viewed 20 times

0

In the application there are 2 login forms, one for customers and another for administrators.

The two forms use the same Prior:

 security:
     providers:
        form_login:
            entity: { class: FMP\SecurityBundle\Entity\User, property: username }

 firewalls:
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false

    app_secured_area:
        pattern: ^/
        anonymous: ~
        form_login:
            provider: form_login
            login_path: security_app_login
            check_path: security_app_login_check
            default_target_path: /
        logout:
            path: security_logout
            target: security_app_login

    admin_secured_area:
          pattern: ^/admin
          anonymous: ~
          form_login:
              provider: form_login
              login_path: security_admin_login
              check_path: security_admin_login_check
              default_target_path: /admin/
          logout:
              path: security_logout
              target: security_admin_login

    default:
        anonymous: ~
        http_basic: ~

I have the DefaultController.php structured as follows:

class DefaultController extends Controller
{
    /**
     * @Route("/login", name="security_app_login")
     * @param Request $request
     * @return \Symfony\Component\HttpFoundation\Response
     */
     public function appLoginAction(Request $request)
     {
        return $this->loginAction($request, 'FMPSecurityBundle:Default:app_login.html.twig');
     }

     /**
      * @Route("/admin/login", name="security_admin_login")
      * @param Request $request
      * @return \Symfony\Component\HttpFoundation\Response
      */
      public function adminLoginAction(Request $request)
      {
           return $this->loginAction($request, 'FMPSecurityBundle:Default:admin_login.html.twig');
      }

      public function loginAction(Request $request, $template)
      {
          $session = $request->getSession();

          if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
              $error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
          } else {
               $error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
          }

          return $this->render($template, array(
              'last_username' => $session->get(SecurityContext::LAST_USERNAME),
              'error'         => $error,
          ));
      }

      /**
       * @Route("/login_check", name="security_app_login_check")
       */
      public function loginCheckAppAction()
      {
          //
      }

      /**
       * @Route("/admin/login_check", name="security_admin_login_check")
       */
      public function loginCheckAdminAction()
      {
          //
      }   

      /**
       * @Route("/logout", name="security_logout")
       */
      public function logoutAction()
      {
          //
      }
}

When accessing the route /admin/login and log in, the following error is returned:

The controller must Return a Response (null Given). Did you Forget to add a Return statement Somewhere in your controller?

What’s wrong? Can’t use the same Preview for different forms?

1 answer

0

First I changed the property check_path of the 2 firewalls:

check_path: security_login_check

This worked but if the administrator enters the wrong credentials, it will be redirected to the client login form, which is an error.

To solve this problem, I changed the order of the firewalls.

Firewalls are called in the order in which they are defined, if the default does not match Symfony will attempt the next pattern.

How I first set the pattern ^/ (ie any request), this one runs first and so Symfony would never run the path ^/admin.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.