Wednesday 15 June 2011

symfony - How to set a login form for admins and another for other users using FOSUserBundle? -


Admin is a backend for users to a login form is interesting, and General English at the same time Forms for general users in the public sector of our website.

Is it possible to use FOSUserBundle? How can this "Symfony2" be the way?

"text" itemprop = "text">

First of all we need to configure some special routes for admin area:

  admin_login: pattern: / admin / login default: {_controller: FOSUserBundle: Security login} admin_login_check: pattern: / admin / login_check defaults: {_controller: FOSUserBundle: Security check} admin_logout: pattern: / admin / logout Oops: {_controller: FOSUserBundle: Security: logout}   

The next administrator uses these routes to specify a region Configure men firewall, and anonymous define them as accessible to be:

  firewalls ... admin: Pattern: /admin/(.*) form_login: Provider: fos_userbundle login_path: admin_login check_path: admin_login_check default_target_path: yourproject_admin_default_index logout path: admin_logout goal: admin_login Unnamed: context: application main pattern: ^ / form_login: provider: fos_userbundle csrf_provider: form.csrf_provider manual application ... Access_control: ... - {path: ^ / admin / login $ role: IS_AUTHENTICATED_ANONYMOUSLY} - {path: ^ / admin / logout $ role: IS_AUTHENTICATED_ANONYMOUSLY} - {path: ^ / admin / Login_check $ role: IS_AUTHENTICATED_ANONYMOUSLY} - {Path: ^ / Admin /, Role: ROLE_ADMIN}   

OK! We have divided our login system into two parts: admin and main.

Override the security controller. For this, we will need to create a custom bundle which is the parent FOSUserBundle (check the doctor for this). In this new bundle, create a controller:

  & lt; Php namespace yourProject \ UserBundle \ Controller; Use FOS \ UserBundle \ Controller \ SecurityController as BaseController; / ** * {@inheritDoc} * / class SecurityController extends BaseController {/ ** * {@inheritDoc} * / public function renderLogin (array $ data) {$ requestAttributes = $ this- & gt; Container- & gt; (Request received ') - & gt; a quality; If ('admin_login' === $ requestAttributes- & gt; get ('_ path')) {$ template = sprintf ('AdminBundle: security: login.html.twig'); } And {$ template = sprintf ('FOSUserBundle: security: login.html.twig'); } $ This- & gt; Container-> Receive ('templating') - & gt; Render Response ($ template, $ data); }}   

That's it! You can now write your AdminBundle: Security: login.html.twig :)

Note: Do not forget to use Admin Routes in your Admin Area! (Login form operation, logout link, etc.)

No comments:

Post a Comment