https://github.com/steevanb/user-bundle
Simple user bundle for Symfony4
https://github.com/steevanb/user-bundle
Last synced: about 1 year ago
JSON representation
Simple user bundle for Symfony4
- Host: GitHub
- URL: https://github.com/steevanb/user-bundle
- Owner: steevanb
- Created: 2018-01-16T15:01:44.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2021-11-29T16:25:51.000Z (over 4 years ago)
- Last Synced: 2025-02-07T18:14:45.133Z (over 1 year ago)
- Language: PHP
- Size: 11.7 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[](https://github.com/steevanb/user-bundle)
# Installation
### Add dependency
```bash
composer require steevanb/user-bundle 0.0.*
# If you want to validate User entity data
composer require symfony/validator symfony/translation
```
### Add bundle
```php
# config/bundles.php
return [
Symfony\Bundle\SecurityBundle\SecurityBundle::class => ['all' => true],
steevanb\\UserBundle\UserBundle::class => ['all' => true]
]
```
### Configure security
```yml
# config/packages/security.yaml
security:
encoders:
App\Entity\User:
algorithm: bcrypt
cost: 12
providers:
database:
entity:
class: App\Entity\User
property: username
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
anonymous: ~
provider: database
form_login:
login_path: login
check_path: login
default_target_path: index
username_parameter: login[username]
password_parameter: login[password]
csrf_parameter: login[_token]
logout:
path: logout
access_control:
- { path: ^/secured-area, roles: ROLE_USER }
```
### Create User entity
Create the entity:
```php
# src/Entity/User.php
namespace App\Entity;
use steevanb\UserBundle\Entity\AbstractUser;
class User extends AbstractUser
{
}
```
Create the mapping:
```yaml
#config/doctrine/User.orm.yml
App\Entity\User:
type: entity
table: user
id:
id:
type: integer
generator: { strategy: AUTO }
options: { unsigned: true }
fields:
username:
length: 50
password:
length: 64
email:
unique: true
roles:
type: array
createdAt:
type: datetime
```
### Create SecurityController
```php
# src/Controller/SecurityController.php
class SecurityController extends steevanb\UserBundle\Controller\AbstractSecurityController
{
protected function createUser(): AbstractUser
{
# Create your User entity
return new User();
}
protected function createRegisterForm(): FormInterface
{
return new RegisterType();
}
protected function createRegisteredResponse(): Response
{
return new Response('User registered.');
}
}
```
### Login
Add route:
```yml
# config/routes.yaml
login:
path: /login
controller: App\Controller\SecurityController::login
```
Create template:
```twig
{# templates/Security/login.html.twig #}
{% if error is not null %}
{{ error }}
{% endif %}
{{ form(formView) }}
```
### Logout
Add route:
```yml
logout:
path: /logout
```
# Enable registration
Registration is not needed and not enabled by default.
Add route:
```yml
# config/routes.yaml
register:
path: /register
controller: App\Controller\SecurityController::register
```
Create RegisterType:
```php
# src/Form/Type/RegisterType.php
namespace App\Form\Type;
use steevanb\UserBundle\Form\Type\AbstractRegisterType;
class RegisterType extends AbstractRegisterType
{
}
```