FOSUserBundle Setup - MiguelFieira/AMO-HANDBOEK GitHub Wiki
Symfony is a PHP framework for web applications and a set of reusable PHP components. Symfony is used by thousands of web applications (including BlaBlaCar.com and Spotify.com) and most of the popular PHP projects (including Drupal and Magento).
NOTICE, FOSUSERBUNDLE HAS BEEN DEPRECATED, YOU CAN USE SYMFONY'S OWN SECURITY BUNDLE FOR USER CREATION Symfony Security Bundle
- Follow the steps 1 & 2A.
- visit the following link: https://symfony.com/doc/4.4/security/form_login_setup.html
- finish the tutorial untill you're at 'controlling error messages'
- Go back to https://symfony.com/doc/4.4/security.html to read more about security functionalities and more
Setup 1. After creating a project and Database you can start adding FOS User Bundle
composer require friendsofsymfony/user-bundle "~2.0" --no-update composer update // This will install all dependencies required for the Bundle
// You should get this error ignore it & continue with the tutorial 2. Now navigate to "src/Entity" and create
"User.php"add the following code.
<?php
// src/Entity/User.php
namespace App\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
}
} 3. Now navigate to "config/packages/security.yaml" and add the following code.
security:
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
encoders:
FOS\UserBundle\Model\UserInterface: bcrypt
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: ROLE_ADMIN
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
providers:
fos_userbundle:
id: fos_user.user_provider.username
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
pattern: ^/
form_login:
provider: fos_userbundle
csrf_token_generator: security.csrf.token_manager
logout: true
anonymous: true
# Easy way to control access for large sections of your site
# Note: Only the *first* access control that matches will be used
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 } // This will add bcrypt encryption and it will also add a role hierarchy. In the example we added a Admin that is above the User (Admin > User) and a Super admin wich is above Admin (Super Admin > Admin). 4. Navigate to "config/packages" and create "fos_user.yaml" and add the following code_
# config/packages/fos_user.yaml
fos_user:
db_driver: orm # other valid values are 'mongodb' and 'couchdb'
firewall_name: main
user_class: App\Entity\User
from_email:
address: "[email protected]"
sender_name: "[email protected]" // This will define the configuration of the user provider. 5. Navigate to "config/packages/framework.yaml" and add the following code
templating:
engines: ['twig', 'php'] // This should enable twig for the FOS User Bundle Result
6. Now navigate to "config/routes" and create the file "fos_user.yaml" add the following code.
fos_user:
resource: "@FOSUserBundle/Resources/config/routing/all.xml" 7. Update your Database Schema (Open your terminal and use the following command)
php bin/console doctrine:schema:update --force 8. Add the super admin to your FOSUserBundle
php bin/console fos:user:create adminuser --super-admin // This will update your database (Check your DB if "fos_user" has been added! Result
8. Now check the following links if everything works! (Assuming your using port:800) Login http://127.0.0.1:8000/login
Register http://127.0.0.1:8000/register
Logout http://127.0.0.1:8000/logout // Only works if your logged in!
// You should get this error ignore it & continue with the tutorial
2. Now navigate to "src/Entity" and create
6. Now navigate to "config/routes" and create the file "fos_user.yaml" add the following code.
8. Now check the following links if everything works! (Assuming your using port:800)
Login
Register
Logout