Php Symfony2在登录后不显示视图登录
我使用bundle FOSUser来管理用户,我的问题是,当您登录时,如果您手动放置路径/登录我将重新显示要登录的表单,我想要的是,如果我放置在浏览器/登录栏上并已登录,则会自动重定向我/索引,而无需我重新显示要登录的表单 你能帮忙吗Php Symfony2在登录后不显示视图登录,php,symfony,Php,Symfony,我使用bundle FOSUser来管理用户,我的问题是,当您登录时,如果您手动放置路径/登录我将重新显示要登录的表单,我想要的是,如果我放置在浏览器/登录栏上并已登录,则会自动重定向我/索引,而无需我重新显示要登录的表单 你能帮忙吗 security: encoders: FOS\UserBundle\Model\UserInterface: sha512 role_hierarchy: ROLE_USER: ROLE_USER ROLE_ADMIN:
security:
encoders:
FOS\UserBundle\Model\UserInterface: sha512
role_hierarchy:
ROLE_USER: ROLE_USER
ROLE_ADMIN: ROLE_ADMIN
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
providers:
fos_userbundle:
id: fos_user.user_provider.username
firewalls:
main:
pattern: ^/
form_login:
check_path: /login_check
login_path: /
provider: fos_userbundle
always_use_default_target_path: false
default_target_path: /index
logout:
path: /logout
target: /
anonymous: ~
#http_basic:
# realm: "Secured Demo Area"
access_control:
- { path: ^/$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin, role: ROLE_SUPER_ADMIN }
- { path: ^/index, role: ROLE_USER, requires_channel: http}
#- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }
我的控制器是:
class SecurityController extends ContainerAware
{
public function loginAction()
{
$request = $this->container->get('request');
$session = $request->getSession();
if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
$error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
} elseif (null !== $session && $session->has(SecurityContext::AUTHENTICATION_ERROR)) {
$error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
$session->remove(SecurityContext::AUTHENTICATION_ERROR);
} else {
$error = '';
}
if ($error) {
$error = $error->getMessage();
}
$lastUsername = (null === $session) ? '' : $session->get(SecurityContext::LAST_USERNAME);
$csrfToken = $this->container->get('form.csrf_provider')->generateCsrfToken('authenticate');
return $this->renderLogin(array(
'last_username' => $lastUsername,
'error' => $error,
'csrf_token' => $csrfToken,
));
}
protected function renderLogin(array $data)
{
$template = sprintf('FOSUserBundle:Security:login.html.%s', $this->container->getParameter('fos_user.template.engine'));
return $this->container->get('templating')->renderResponse($template, $data);
}
public function checkAction()
{
throw new \RuntimeException('You must configure the check path to be handled by the firewall using form_login in your security firewall configuration.');
}
public function logoutAction()
{
throw new \RuntimeException('You must activate the logout in your security firewall configuration.');
}
}
在登录控制器中,您可以执行类似操作
if($this->getUser() instanceof your_user_entity){
return $this->redirectToRoute('route_of_your_index');
}
或其他
将您的安全系统编辑为自动的请提供您的security.yml文件contentsOk,它只是编辑消息。我在尝试调用类“Acme\UserBundle\Controller\SecurityController”上的方法“getUser”时收到此错误。您的控制器需要扩展控制器