Symfony 2,登录后FOSUserBundle重定向

时间:2016-10-27 16:07:30

标签: php symfony redirect login fosuserbundle

我想知道如何在登录后设置重定向?我有2个控制器,一个用于未经授权的和常规用户,另一个用于管理员用户,我希望我的登录表单在以管理员身份登录后立即从管理员控制器重定向到路径。 控制器的一小部分: 定期

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,
    );
}}

管理:

/**
 * 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,
    ));
}

我的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"

我的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 }

提前感谢所有建议。

编辑:下面发布的链接中的问题不适用于FOSU​​serBundle。

1 个答案:

答案 0 :(得分:0)

将此功能添加到您的一个控制器中

    /**
     * @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());
        }
    }

,在“防火墙”部分和“主要”部分的security.yml中,我添加

 always_use_default_target_path: false 
 default_target_path: /app_admin_buty_index