Installation
Add the bundle to your composer.json:
composer require customscripts/user-bundle
Register the bundle in config/bundles.php (Symfony) or AppServiceProvider (Laravel via Bridge):
// Laravel (via Bridge)
$this->mergeConfigFrom(__DIR__.'/../../vendor/customscripts/user-bundle/config/config.php', 'user-bundle');
Publish Config Publish the default configuration:
php artisan vendor:publish --provider="CustomScripts\UserBundle\CSUserBundleServiceProvider"
Update config/user-bundle.php as needed (e.g., guard_name, model_paths).
Run Migrations Execute the provided migrations:
php artisan migrate
(Note: Check database/migrations/ for users_table and roles_table if not auto-generated.)
First Use Case: User Registration
Use the UserManager facade to create a user:
use CustomScripts\UserBundle\Facades\UserManager;
$user = UserManager::create([
'name' => 'John Doe',
'email' => 'john@example.com',
'password' => 'secure123',
]);
Authentication
Auth facade (if provided) or leverage Laravel’s built-in Auth::attempt().auth:user-bundle (if the bundle registers middleware).
Route::get('/dashboard', function () {
// ...
})->middleware('auth:user-bundle');
Role-Based Access Control (RBAC)
$user->assignRole('admin'); // Assuming `Role` model methods exist.
if (auth()->user()->hasRole('editor')) {
// Grant access.
}
Gate or Policy classes to use the bundle’s role checks:
Gate::define('edit-post', function ($user) {
return $user->hasRole('admin') || $user->hasRole('editor');
});
Password Reset
UserManager::sendPasswordResetLink('user@example.com');
Route::post('/reset-password', [ResetPasswordController::class, 'handleReset']);
User CRUD
$users = UserManager::all();
UserManager::update($user->id, ['email' => 'new@example.com']);
UserCreated, RoleAssigned) via Laravel’s event system:
event(new UserCreated($user));
UserResource (if provided):
return new UserResource(UserManager::find($id));
UserManager facade in tests:
$this->app->instance('CustomScripts\UserBundle\Facades\UserManager', Mockery::mock());
Archived Status
Configuration Quirks
config/user-bundle.php sets guard_name to match your auth guard (e.g., web or api).model_paths in config:
'models' => [
'user' => App\Models\User::class,
'role' => App\Models\Role::class,
],
Migration Conflicts
users table. Backup your database before running:
php artisan migrate --pretend
role_id to Laravel’s users table).Facade vs. Service Container
// Bad (facade)
$user = UserManager::find(1);
// Good (injected)
public function __construct(UserManager $userManager) {
$this->userManager = $userManager;
}
Role Assignment
admin > editor), implement logic manually:
$user->roles()->attach('admin', ['inherits' => ['editor']]);
Enable Logging
Add to config/logging.php to debug bundle interactions:
'channels' => [
'user-bundle' => [
'driver' => 'single',
'path' => storage_path('logs/user-bundle.log'),
'level' => 'debug',
],
],
Then log events:
\Log::channel('user-bundle')->info('Custom event', ['user_id' => $user->id]);
Check for Deprecated Methods
Auth::user() instead of auth()->user()). Override methods in a trait:
use CustomScripts\UserBundle\Traits\UserTrait;
class User extends Authenticatable {
use UserTrait {
UserTrait::getAuthUser insteadOf getUser;
}
}
Database Seed Issues
tinker:
php artisan tinker
>>> $role = \App\Models\Role::create(['name' => 'admin']);
>>> $user = \App\Models\User::create(['email' => 'admin@example.com']);
>>> $user->assignRole($role);
Custom User Model
Extend the bundle’s User model:
namespace App\Models;
use CustomScripts\UserBundle\Models\User as BaseUser;
class User extends BaseUser {
protected $casts = [
'is_active' => 'boolean', // Custom field
];
}
Add Fields to Users Modify the migration or use a trait to add columns:
Schema::table('users', function (Blueprint $table) {
$table->string('phone')->nullable();
});
Override Auth Logic Replace the bundle’s auth controller with a custom one:
// config/auth.php
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
],
API Endpoints If the bundle lacks API routes, create them manually:
Route::post('/api/register', [RegisterController::class, 'store']);
How can I help you explore Laravel packages today?