Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/steevanb/user-bundle
Simple user bundle for Symfony4
https://github.com/steevanb/user-bundle
Last synced: 9 days 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 (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2021-11-29T16:25:51.000Z (about 3 years ago)
- Last Synced: 2024-11-27T18:22:16.197Z (26 days 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
[![version](https://img.shields.io/badge/version-dev-red.svg)](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.phpnamespace App\Entity;
use steevanb\UserBundle\Entity\AbstractUser;
class User extends AbstractUser
{
}```
Create the mapping:
```yaml
#config/doctrine/User.orm.ymlApp\Entity\User:
type: entity
table: userid:
id:
type: integer
generator: { strategy: AUTO }
options: { unsigned: true }fields:
username:
length: 50password:
length: 64email:
unique: trueroles:
type: arraycreatedAt:
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.phpnamespace App\Form\Type;
use steevanb\UserBundle\Form\Type\AbstractRegisterType;
class RegisterType extends AbstractRegisterType
{
}
```