Installation
composer require dayspring-tech/login-bundle
Enable the bundle in config/bundles.php:
return [
// ...
Dayspring\LoginBundle\DayspringLoginBundle::class => ['all' => true],
];
Configure security.yml
Copy the provided security.yml snippet into your config, replacing placeholders with your actual:
App\Entity\User).default_target_path).firewalls to match your routing.First Use Case: Basic Login
{{ form_start(form) }}
{{ form_widget(form._username) }}
{{ form_widget(form._password) }}
<button type="submit">Login</button>
{{ form_end(form) }}
_login_check (default path: /login_check).Verify Routes Run:
php bin/console debug:router | grep login
Ensure _login, _login_check, _logout, and _forgot_password routes exist.
User Authentication Flow
_login_check (POST). Bundle handles validation and authentication._logout (GET/POST). Bundle clears session and redirects to target._forgot_password (POST).dayspring_login.mailer (customizable)._reset_password (POST).Custom User Model
Extend the default user provider by implementing Dayspring\LoginBundle\Provider\UserProviderInterface:
// src/Service/CustomUserProvider.php
class CustomUserProvider implements UserProviderInterface {
public function loadUserByUsername($username) {
return $this->entityManager->getRepository(User::class)->findOneBy(['email' => $username]);
}
}
Register as a service in services.yaml:
services:
dayspring_login.user_provider:
class: App\Service\CustomUserProvider
Password Change
_change_password route (POST) with a form:
{{ form_start(form) }}
{{ form_widget(form.currentPassword) }}
{{ form_widget(form.newPassword) }}
{{ form_widget(form.confirmPassword) }}
{{ form_end(form) }}
dayspring_login Twig functions for user checks:
{% if app.user %}
Welcome, {{ app.user.username }}!
{% endif %}
dayspring_login.security.event for custom logic (e.g., post-login redirects):
// src/EventListener/CustomLoginListener.php
class CustomLoginListener {
public function onLoginSuccess(LoginSuccessEvent $event) {
if ($event->getUser()->isAdmin()) {
$event->setTargetPath('/admin');
}
}
}
Register in services.yaml:
services:
App\EventListener\CustomLoginListener:
tags:
- { name: kernel.event_listener, event: dayspring_login.security.login_success, method: onLoginSuccess }
Routing Conflicts
_login_check and _forgot_password routes don’t clash with existing routes. Use _ prefix to avoid conflicts with Symfony’s profiler routes.config/routes.yaml:
_login:
path: /login
controller: Dayspring\LoginBundle\Controller\SecurityController::login
User Provider Mismatch
loadUserByUsername returns null, the bundle throws a UsernameNotFoundException.loadUserByUsername logic matches your user entity’s unique field (e.g., email vs. username).Password Hashing
bcrypt with cost 12. For legacy systems, adjust password_hashers in security.yml:
password_hashers:
App\Entity\User:
algorithm: auto # Auto-detects existing hashes
Email Templates
templates/DayspringLoginBundle/Mail/. Override them by copying to templates/your_bundle/Mail/ and extending the base template:
{# templates/App/Mail/reset_password.html.twig #}
{% extends 'DayspringLoginBundle:Mail:reset_password.html.twig' %}
{% block subject %}{{ 'Custom Reset Subject'|trans }}{% endblock %}
debug: true to security.yml to log authentication attempts:
security:
debug: true
bin/console debug:event-dispatcher to verify listeners are registered for:
dayspring_login.security.login_successdayspring_login.security.login_failuredayspring_login.mailer.reset_sentCustom Mailer Override the default mailer service:
services:
dayspring_login.mailer:
class: App\Service\CustomMailer
arguments:
$transport: '@mailer.transport.dsn'
Implement Dayspring\LoginBundle\Mailer\MailerInterface.
User Checker
Extend Dayspring\LoginBundle\Security\UserChecker to add custom checks (e.g., account expiration):
class CustomUserChecker extends UserChecker {
public function checkPreAuth(UserInterface $user) {
if ($user->isSuspended()) {
throw new AccountStatusException('Account suspended.');
}
}
}
Register in services.yaml:
services:
dayspring_login.user_checker:
class: App\Security\CustomUserChecker
Form Customization Extend the default login form by overriding the template:
{# templates/App/Security/login.html.twig #}
{% extends 'DayspringLoginBundle:Security:login.html.twig' %}
{% block form_fields %}
{{ form_row(form.remember_me) }}
{{ parent() }}
{% endblock %}
Propel Inheritance
For UserProfile models, ensure Propel schema is updated:
<behavior name="propel">
<parameter name="model" value="UserProfile" />
<parameter name="parent" value="User" />
</behavior>
Regenerate models:
php bin/console propel:build --all
How can I help you explore Laravel packages today?