How to create authentication in symfony ?


Symfony is providing us a bundle friend of symfony (FOSUserBundle) to authenticate our application.

FOSUser bundle is the powerful bundle to manage user of our application, symfony is most powerful and poplar php framework.

To create authentication follow this steps.

To install symfony applocation 

 symfony new my_project_name

To install FOSUser bundle

$ composer require friendsofsymfony/user-bundle "~2.0"

To enable the bundle


<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new FOS\UserBundle\FOSUserBundle(),
        // ...
    );
}

To create user model or class of your application.


Create a file directory of application src/AppBundle/Entity/User.php

<?php

namespace AppBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="fos_user")
 */
class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    public function __construct()
    {
        parent::__construct();
        // your own logic
    }
}

Configure your application security.

Change the file content directory of app/config.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 }



Configure your fos user bundle.

Change the file content directory of app/config.config.yml

fos_user:
    db_driver: orm # other valid values are 'mongodb' and 'couchdb'
    firewall_name: main
    user_class: AppBundle\Entity\User
    from_email:
        address: "%mailer_user%"
        sender_name: "%mailer_user%"

Import fos user bundle routing file 

Add route file in you application app/config.route.yml

fos_user:
    resource: "@FOSUserBundle/Resources/config/routing/all.xml"

Create database of the application 


$ php bin/console doctrine:database:create

Update your database schema 


$ php bin/console doctrine:schema:update --force




Your fos user bundle has been configured now you can create your user and

link for create user

http://127.0.0.1:8000/register

No comments

Powered by Blogger.