问题
I am having a very strange problem with symfony2 and the FOSUserBundle.
I can logout with /en/logout, but not with /nl/logout or /fr/logout.
When I try to logout with nl or fr I get:
You must activate the logout in your security firewall configuration.
Although I configured it. I can't seem to wrap my head why the /en/logout works and the rest doesn't.
This is my code:
security.yml
security:
providers:
fos_userbundle:
id: fos_user.user_provider.username_email
encoders:
FOS\UserBundle\Model\UserInterface: sha512
firewalls:
main:
pattern: ^/
form_login:
provider: fos_userbundle
csrf_provider: form.csrf_provider
check_path: fos_user_security_check
default_target_path: /%locale%/login
always_use_default_target_path: true
failure_path: /%locale%/login
logout:
path: /%locale%/logout
target: homepage
anonymous: true
routing.yml
user bundle > FOS
fos_user_security:
resource: "@FOSUserBundle/Resources/config/routing/security.xml"
prefix: /{_locale}
requirements:
_locale: fr|nl|en
controller
class LoginController extends Controller {
/**
* @Route("{_locale}/logout-test", name="logout", defaults={"_locale"="en"} , requirements = {"_locale" = "fr|en|nl"})
* @Template()
*/
public function logoutAction()
{
$test = "";
#throw new \RuntimeException('You must activate the logout in your security firewall configuration.');
#return $this->redirect($this->generateUrl('homepage'));;
}
}
Can anyone help me out, or tell me where to look next? It would be much appreciated
回答1:
In your configuration file, you need to use routes instead of URLs. If it starts with a "/" it will be treated as an URL, else it will be treated as a route. If you use routes instead of URLs, the locale will be taken care of automatically. For example, here is my security.yml configuration:
security:
public:
pattern: ^/
form_login:
login_path: fos_user_security_login
check_path: fos_user_security_check
provider: fos_userbundle
csrf_provider: form.csrf_provider
default_target_path: index
anonymous: true
logout:
path: fos_user_security_logout
target: index
回答2:
I leave this for future reference, in addition to @jfcartier's answer:
In case you need a custom logout
path instead of fos_user_security_logout
:
# app/config/security.yml
...
logout:
path: my_logout
target: homepage
... then you also need to define it in routing.yml
:
# app/config/routing.yml
app:
resource: "@AppBundle/Controller/"
type: annotation
my_logout:
path: /logout
fos_user:
resource: "@FOSUserBundle/Resources/config/routing/all.xml"
Though, make sure that it's defined before fos_user
- otherwise you'll get the same error: You must activate the logout in your security firewall configuration.
来源:https://stackoverflow.com/questions/14941989/fosuserbundle-logout-with-prefix-doesnt-work