para symfony 3.4
instalar antes:
composer require symfony/templating
luego en config.yml
framework:
templating:
engines: ['twig']
descargar el bundle, (para symfony3.0+)
$ composer require friendsofsymfony/user-bundle "~2.0"
El objetivo de este bundle es persistir una clase User en una base de datos.
#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
}
}
en security.yml :
# To get started with security, check out the documentation:
# http://symfony.com/doc/current/security.html
security:
encoders:
FOS\UserBundle\Model\UserInterface: bcrypt
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: ROLE_ADMIN
# http://symfony.com/doc/current/security.html#b-configuring-how-users-are-loaded
providers:
fos_userbundle:
id: fos_user.user_provider.username
in_memory:
memory: ~
firewalls:
# disables authentication for assets and the profiler, adapt it according to your needs
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
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
#anonymous: ~
# activate different ways to authenticate
# http://symfony.com/doc/current/security.html#a-configuring-how-your-users-will-authenticate
#http_basic: ~
# http://symfony.com/doc/current/cookbook/security/form_login_setup.html
#form_login: ~
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 }
agregar en 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%"
donde from_email indica los datos a traves de quien se envia el mail de confirmacion al momento de registrarse.
por lo que hay que registrar ese valor
en parameters.yml:
mailer_user: alguncorreo@lalala.com
agregar en routing.yml :
fos_user:
resource: "@FOSUserBundle/Resources/config/routing/all.xml"
creamos la tabla user por comando:
php bin/console doctrine:schema:update --force
ahora podemos crear un usuario por comando del bundle:
php bin/console fos:user:create
y para loguearnos vamos a usar como username nuestro email, entonces modificamos el security.yml
security:
providers:
fos_userbundle:
id: fos_user.user_provider.username_email
ya podemos probar el login:
http://localhost/necosocial/web/app_dev.php/login