15

I begin with Symfony 4 and I want to install FosUserBundle with this link : https://symfony.com/doc/master/bundles/FOSUserBundle/index.html

First :

My problem is that I don't know where to find the "app/config/config.yml" file to uncomment the translator and to configure :

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%"

Second :

I think that I have to create the security.yml file in "config/packages/" directory, is that right ?

Third :

And in which file to add the route ?

Could you help me, please ? :)

cretthie
  • 349
  • 1
  • 2
  • 11
  • 5
    As far as I know, the FOSUserBundle is still not supporting Symfony 4 :( – Angelov Dec 16 '17 at 11:43
  • 1
    On the one hand, FOSUserBundle is the most widely used third party bundle out there. On the other, it has a history of being largely unmaintained and over engineered. Unless you really really really need to support multiple types of databases then you might be better off just following the Symfony docs and implementing your own system. There is really not much to it and you will learn a great deal. – Cerad Dec 16 '17 at 15:31
  • Not a criticism of your suggestion @Cerad which I think is the way to go for large projects, but the OP should also note that he'll miss out on many other bundles that integrate with FOSUser. – Don Omondi Dec 17 '17 at 01:42
  • @DonOmondi Yes there are other bundles which rely on the FOSUserBundle. Alas, I'm not very impressed with them either. I occasionally look through their code and steal ideas from them, but avoid actually using them. – Cerad Dec 17 '17 at 14:51

5 Answers5

19

I've resolved the problem followed this:

  1. download FOSUserBundle using composer:

    composer require friendsofsymfony/user-bundle "~2.0"

At the end of the installation you will have the following error message :

The child node "db_driver" at path "fos_user" must be configured.

  1. Create your User class Create src/Entity/User.php as custom user class who extend the FOSUserBundle BaseUser class.
 <?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
    }
}
  1. Configure your application's security.yml Modify config/packages/security.yaml to setup FOSUserBundle security
    security:
    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 }
  1. Configure the FOSUserBundle Create a new file config/packages/fos_user.yaml for the configuration of FOSUserBundle
fos_user:
db_driver: orm # other valid values are 'mongodb' and 'couchdb'
firewall_name: main
user_class: App\Entity\User
from_email:
    address: "vincent@vfac.fr"
    sender_name: "vincent@vfac.fr"

Update config/packages/framework.yaml to add templating configuration

framework:
    templating:
        engines: ['twig', 'php']
  1. Import FOSUserBundle routing Create config/routes/fos_user.yaml
fos_user:
resource: "@FOSUserBundle/Resources/config/routing/all.xml"
  1. Update your database schema If not already done, you must create your database

php bin/console doctrine:database:create

Update the schema with the informations from your User class entity

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

At this point, all is installed and configured to use FOSUserBundle in Symfony 4. Run the following command to check if all is ok

composer update

If you don't have any error message, you can test ! You can run the web server to test your application

php bin/console server:start

all tutorial here: https://vfac.fr/blog/how-install-fosuserbundle-with-symfony-4

Chad
  • 1,139
  • 17
  • 41
elfTine
  • 299
  • 2
  • 5
14

This is the solution I found to work.

First:

app/config/config.yml doesn't exist anymore instead the configs have been moved to the config folder. For the FOS User Bundle the correct location: /config/packages/fos_user.yaml. As already noted, use the dev-master version FOSUserBundle, it supports 4(still a little work in progress but good enough).

Second:

You are correct, a simple solution is do a composer require security and the recipe will take care of that for you. https://symfony.com/doc/current/security.html for more info.

Third:

The default FOS User Bundle routes:

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

More info on the FOS routing (step 6)is helpful https://symfony.com/doc/master/bundles/FOSUserBundle/index.html

Also, I recommend looking at the yaml samples in symfony routing documentation. It may make things a little more clear when configuring the routes with relation to FOS User Bundle. https://symfony.com/doc/current/routing.html

Zoe
  • 27,060
  • 21
  • 118
  • 148
Sean Bahrami
  • 161
  • 3
  • 2
    @DarraghEnright Fos user bundle 2.1 now supports symfony 4. I recommend using it. I did get errors with the recipe, but it’s very simple to resolve once you initially set up bundle. – Sean Bahrami Mar 20 '18 at 15:07
6

ok i have same problem and it should be like that first:as @sean Baharmi says you should create /config/packages/fos_user.yaml and inter configuration like this

fos_user:
  db_driver: orm
  firewall_name: main
  user_class: App\Entity\Users
  from_email:
      address: "hello@youmail.com"
      sender_name: "Sender Name"

then in framework.yaml you should enter following because of FOSUserBundle dependencies

templating:
  engines: ['twig', 'php']

also for add routing in /config/rourtes/routes.yaml add

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

then it is ready to work

hope works for you

Imanali Mamadiev
  • 2,604
  • 2
  • 15
  • 23
Ghazaleh Javaheri
  • 1,829
  • 19
  • 25
2

You can't use FOSUSerBundle on Symfony4 at the moment. Support has not been merged yet. You can follow development here.

dlondero
  • 2,539
  • 1
  • 24
  • 33
  • 1
    A few days later support has been merged to master, but not tagged yet. You can use it setting your dependency on `dev-master` if you like to live on the bleeding edge and can't wait for it :P https://github.com/FriendsOfSymfony/FOSUserBundle/pull/2639 – dlondero Dec 22 '17 at 11:22
0

If you want to use FOSUserBundle with Symfony4 you can try the patch Ryan provided here.

kunicmarko20
  • 2,095
  • 2
  • 15
  • 25