Installation
composer require alpixel/userbundle:~2.0
Ensure compatibility with your Symfony version (2.8+ or 3.0).
Register the Bundle
Add to AppKernel.php:
new Alpixel\Bundle\UserBundle\AlpixelUserBundle(),
Create a Custom User Entity
Extend BaseUser in your AppBundle:
namespace AppBundle\Entity;
use Alpixel\Bundle\UserBundle\Entity\User as BaseUser;
class User extends BaseUser { /* ... */ }
Update Database Schema
php app/console doctrine:schema:update --force
Add Routing
Include in app/config/routing.yml:
admin:
resource: '@AlpixelUserBundle/Resources/config/routing.yml'
Configure Security (Optional)
Override security.yml if using multiple firewalls. Copy from the bundle’s Resources/config/security.yml and customize.
Load Fixtures The bundle includes fixtures for admin users. Run:
php app/console doctrine:fixtures:load --no-interaction
(Ensure AlpixelUserBundle fixtures are enabled in app/config/config.yml.)
Access Admin Panel
Navigate to /admin (default route). Use the pre-created admin credentials (check fixtures for defaults).
Custom Fields
Add fields to your User entity (e.g., role, profilePicture):
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $profilePicture;
Update the BaseUser abstract class if shared across projects.
Sonata Admin Integration
Extend AlpixelUserBundle's admin class for Sonata:
namespace AppBundle\Admin;
use Alpixel\Bundle\UserBundle\Admin\UserAdmin as BaseUserAdmin;
class UserAdmin extends BaseUserAdmin { /* ... */ }
Register in services.yml:
services:
app.admin.user:
class: AppBundle\Admin\UserAdmin
tags:
- { name: sonata.admin, manager_type: orm, group: "Users", label: "User" }
Custom Login Templates
Override templates in app/Resources/AlpixelUserBundle/views/admin/:
alpixel_user:
firewall_templates:
admin:
login_template: 'AlpixelUserBundle:admin:custom_login.html.twig'
Dynamic Firewall Configuration
Use the alpixel_user.firewall_templates parameter to customize login pages per firewall:
alpixel_user:
firewall_templates:
api:
login_template: 'AlpixelUserBundle:api:login.html.twig'
FOSUserBundle Compatibility:
Leverage FOSUserBundle’s features (e.g., UserManager, UserInterface) via BaseUser.
Example:
$userManager = $this->get('fos_user.user_manager');
$user = $userManager->createUser();
Sonata Admin Customization:
Override Sonata templates for the admin panel (e.g., edit.html.twig) in your theme.
Fixtures:
Extend AlpixelUserBundle\DataFixtures\ORM\LoadUserData to add custom fixtures:
class LoadCustomUserData extends LoadUserData {
public function load(ObjectManager $manager) {
parent::load($manager);
// Add custom users...
}
}
Deprecated FOSUserBundle Version:
The bundle targets FOSUserBundle ~1.3 or ~2.0. Ensure your composer.json aligns:
"friendsofsymfony/user-bundle": "^2.0"
If using FOSUserBundle 3.x, fork the bundle or patch compatibility.
Schema Updates:
Running doctrine:schema:update without --force may fail if the BaseUser schema changes. Backup your database first.
Sonata Admin Conflicts:
If Sonata Admin is not loaded, the admin routes may break. Ensure SonataAdminBundle is registered before AlpixelUserBundle.
Fixtures Overwriting:
Default fixtures create an admin user with hardcoded credentials (admin/admin). Override in LoadUserData or disable fixtures:
# app/config/config.yml
alpixel_user:
fixtures: false
Routing Issues: Clear cache and check routes:
php app/console cache:clear
php app/console debug:router | grep admin
Template Overrides: Verify template paths. Use Symfony’s debug toolbar to confirm template inheritance.
Firewall Configuration:
If login fails, check security.yml for correct firewall names and template paths:
firewalls:
admin:
pattern: ^/admin
form_login:
template: 'AlpixelUserBundle:admin:pages/back_login.html.twig'
Custom User Events:
Listen to FOSUserBundle events (e.g., fos_user.registered) to extend logic:
services:
app.user.event_listener:
class: AppBundle\EventListener\UserListener
tags:
- { name: kernel.event_listener, event: fos_user.registered, method: onUserRegistered }
Dynamic Login Styling: Override CSS/JS in your theme by extending the bundle’s assets:
{# app/Resources/public/css/admin.css #}
.alpixel-login { /* Custom styles */ }
Multi-Tenant Support:
Extend BaseUser to add tenant-specific fields (e.g., tenantId) and filter queries in repositories.
API Integration:
Use the api firewall template to create a lightweight login endpoint for SPAs:
alpixel_user:
firewall_templates:
api:
login_template: 'AlpixelUserBundle:api:login.json.twig'
How can I help you explore Laravel packages today?