Question

I'm having some issues working with FOSUserBundle. I find the documentation lacking and think it would really help if they had an example application that I could download the source code for and look at.

My login route is /Login. I've overrode the twig template so that I get a login form displaying when I navigate to my route. However I had to add some default values into my LoginAction to make it work.

My LoginAction:

/**
     * @Route("/Login", name="wx_exchange_login")
     * @Template("WXExchangeBundle:User:login.html.twig")
     * User log up - Open to public
     * Authenticates users to the system
     */
    public function loginAction()
    {
        return array('error' => 0, 'csrf_token' => 'foobar', 'last_username' => 'foobar');
    }

FOSUserBundle LoginAction

I was under the impression that since FOSUserBundle had the Action code already defined I wouldn't need to have any code in my loginAction. It's not clear from the docs that I am supposed to do anything in my action. I've seen some examples online showing people having copied the code from relevant FOSUserBundle action into their own action.

Is the correct way to implement FOSUserBundle to copy their code into your own actions?

Edit: I am not overriding the bundle.

Edit 2: My routing.yml is slightly different from the docs. My first entry is:

wx_exchange:
    resource: "@WXExchangeBundle/Controller/"
    type:     annotation
    prefix:   /

The remaining entries are as stated in the docs (importing the FOSUserBundle routes).

Security.yml:

# app/config/security.yml
security:
    encoders:
        FOS\UserBundle\Model\UserInterface:
            algorithm: bcrypt
            cost: 15

    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]

    providers:
        fos_userbundle:
            id: fos_user.user_provider.username_email

    firewalls:
        main:
            pattern: ^/
            form_login:
                provider: fos_userbundle
                csrf_provider: form.csrf_provider
                login_path: /Login
            logout:       true
            anonymous:    true

    access_control:
        - { path: ^/Login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/SignUp, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/ResetPassword, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/Confirm, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/Dashboard/, role: ROLE_ADMIN }
Was it helpful?

Solution

I might have finally got an answer to this. The code for the login part is indeed already defined in the FOSUserBundle. I looked at the routing of my own project and can see that it looks like this:

fos_user_security_login:
    pattern: /login
    defaults: { _controller: FOSUserBundle:Security:login }

//Check a login:
fos_user_security_check:
    pattern: /login_check
    defaults: { _controller: FOSUserBundle:Security:check }
    methods: [POST]

//The logout route:
fos_user_security_logout:
    pattern: /logout
    defaults: { _controller: FOSUserBundle:Security:logout }

Now, these routes all point to the SecurityController of the bundle with those methods in it. This class can be found in /vendor/friendsofsymfony/user-bundle/FOS/UserBundle/Controller/SecurityController.php and the routing can be found in /vendor/friendsofsymfony/user-bundle/FOS/UserBundle/Resources/config/routing/security.xml (I converted it in this post to YAML for easier understanding).

So, the only thing you need to do is do a reference (import) in the routing file in the app folder to this security.yml file.

To answer your question: I don't think you need to define these in your own controller as they're already defined in the above mentioned files.

If I can find more details I'll post them ASAP. Been a while that I used this bundle. I now use the builtin Symfony2 elements, which I find more transparent in the long run.

Edit: In the routing file of my own bundle I have the references to those XML files:

// FOSUser files
fos_user_security:
    resource: "@FOSUserBundle/Resources/config/routing/security.xml"

fos_user_profile:
    resource: "@FOSUserBundle/Resources/config/routing/profile.xml"
    prefix: user/profile

fos_user_register:
    resource: "@FOSUserBundle/Resources/config/routing/registration.xml"
    prefix: user/register

fos_user_resetting:
    resource: "@FOSUserBundle/Resources/config/routing/resetting.xml"
    prefix: user/resetting

fos_user_change_password:
    resource: "@FOSUserBundle/Resources/config/routing/change_password.xml"
    prefix: user/profile
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top