1
votes

I wonder how to set redirection after login? I've got 2 Controllers, one for unauthorized and regular users, and another one for admin users, and I want my login form do redirect to paths from admin controller immediately after logging in as an admin. Small parts of my controllers: Regular

class DefaultController extends Controller
{
/**
 * @Route("/", name="app_front_default_index")
 * @Template()
 */
public function indexAction(Request $request)
{
    $em = $this->getDoctrine()->getManager();

    $shoes = $em->getRepository("ShoeShopBundle:Buty")->findAll();

    $paginator = $this->get('knp_paginator');
    $pagination = $paginator->paginate(
        $shoes,
        $request->get('page',1),
        12
);
    return array(
        'pagination' => $pagination,
    );
}}

Admin:

/**
 * Buty controller.
 * @Route("/buty")
 */
class ButyController extends Controller
{
/**
 * Lists all Buty entities.
 *
 * @Route("/", name="app_admin_buty_index")
 * @Method("GET")
 */
public function indexAction()
{
    $em = $this->getDoctrine()->getManager();

    $shoes = $em->getRepository('ShoeShopBundle:Buty')->findAll();

    return $this->render('ShoeShopBundle:Admin/Buty:index.html.twig', array(
        'shoes' => $shoes,
    ));
}

My routing.yml

app_admin:
  resource: "@ShoeShopBundle/Controller/Admin/"
  type: "annotation"
  prefix: "/admin"

app_front:
  resource: "@ShoeShopBundle/Controller/Front/"
  type: "annotation"
  prefix: "/"

fos_user:
    resource: "@FOSUserBundle/Resources/config/routing/all.xml"

And my security.yml

security:
encoders:
    FOS\UserBundle\Model\UserInterface: bcrypt

role_hierarchy:
    ROLE_ADMIN:       ROLE_USER
    ROLE_SUPER_ADMIN: ROLE_ADMIN

providers:
    fos_userbundle:
        id: fos_user.user_provider.username

firewalls:
    main:
        pattern: ^/
        form_login:
            provider: fos_userbundle
            csrf_token_generator: security.csrf.token_manager
            # if you are using Symfony < 2.8, use the following config instead:
            # csrf_provider: form.csrf_provider

        logout:       true
        anonymous:    true

access_control:
    - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/admin, role: ROLE_ADMIN }

Thanks in advance for all suggestions.

EDIT: The question in link posted below does not apply to FOSUserBundle.

1

1 Answers

0
votes

add this function in one of your controller

    /**
     * @Route("/", name="app_admin_buty_index")
     */
    public function redirectAction()
    {
        $authChecker = $this->container->get('security.authorization_checker');

        if ($authChecker->isGranted('ROLE_USER')) {
            return $this->render('the name of your user homePage', array());

        } elseif ($authChecker->isGranted('ROLE_ADMIN ')) {
            return $this->render('the name of your admin homePage ',array());

        } else {
            return $this->render('userLogin.html.twig', array());
        }
    }

and in security.yml in firewalls section and in main section i add this

 always_use_default_target_path: false 
 default_target_path: /app_admin_buty_index