如果用户从本教程登录,我尝试从/ login进行重定向:https://codereviewvideos.com/course/getting-started-with-fosuserbundle/video/ux-improvements-part-1-redirect-login-when-logged-in
但是现在我得到代码302的无限循环,这是我的代码:
<?php
// src/crmBundle/Controller/SecurityController.php
namespace crmBundle\Controller;
use FOS\UserBundle\Controller\SecurityController as BaseController;
use Symfony\Component\HttpFoundation\Request;
use FOS\UserBundle\Model\User;
use Symfony\Component\HttpFoundation\RedirectResponse;
class SecurityController extends BaseController
{
public function loginAction(Request $request)
{
$user = $this->get('security.token_storage')->getToken()->getUser();
if ($user instanceof User) {
return new RedirectResponse(
$this->generateUrl('dashboard')
);
}
return parent::loginAction($request);
}
}
// src/crmBundle/crmBundle.php
namespace crmBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class crmBundle extends Bundle
{
public function getParent()
{
return 'FOSUserBundle';
}
}
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: false
access_control:
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/, role: ROLE_USER }
问题已解决。