Installation
composer require bisonlab/user-bundle
php artisan vendor:publish --provider="BisonLab\UserBundle\UserBundle" --tag="config"
php artisan vendor:publish --provider="BisonLab\UserBundle\UserBundle" --tag="migrations"
php artisan migrate
Generate User Model & Controller
Since the bundle prefers maker-bundle (a common Laravel package), ensure it’s installed:
composer require --dev orbitale/maker-bundle
php artisan make:user User --bundle=BisonLab\UserBundle
This creates a User model, controller, and related files with preconfigured traits.
First Use Case
AuthController (or extend it) for login/logout.AuthController to handle registration via the User model.@auth or @guest directives in Blade templates (if using Laravel’s auth system).config/user-bundle.php (customize user model, guards, and providers).database/migrations/[timestamp]_create_users_table.php (adjust fields as needed).app/Models/Traits/UserTrait.php (core logic like findByEmail() or validatePassword()).User Creation & Registration
// Extend AuthController or create a custom service
$user = User::create([
'email' => 'user@example.com',
'password' => bcrypt('password123'),
'name' => 'John Doe',
]);
User model’s built-in rules (e.g., unique:users,email).Authentication
AuthController@login() to use the bundle’s authenticate() method.auth()->logout() (standard Laravel).Authorization
User model with hasRole() or can() methods (not built-in; add via traits).Route::get('/admin', function () {
// ...
})->middleware('can:admin-access'); // Requires custom middleware
API Integration
Sanctum or Passport alongside the bundle for API auth.config/user-bundle.php:
'guards' => [
'api' => [
'driver' => 'sanctum',
'provider' => 'users',
],
],
users table via migrations, then update the User model’s $fillable.Creating, Created, etc.:
// app/Providers/EventServiceProvider.php
protected $listen = [
'BisonLab\UserBundle\Events\UserCreated' => [
'App\Listeners\SendWelcomeEmail',
],
];
actingAs() or fake() for auth tests:
$user = User::factory()->create();
$this->actingAs($user)->get('/profile');
Maker-Bundle Dependency
maker-bundle for scaffolding. If missing, manually create the User model/controller or adjust the bundle’s setup.orbitale/maker-bundle or generate files manually.Migration Conflicts
users table already exists, the bundle’s migrations may fail. Use --force or resolve column conflicts:
php artisan migrate --force
database/migrations/[timestamp]_create_users_table.php for required fields (email, password, etc.).Auth Guard Misconfiguration
session guard. For API auth, update config/user-bundle.php:
'defaults' => [
'guard' => 'api', // Change from 'web' to 'api'
],
Password Hashing
bcrypt hashing. For custom hashing (e.g., Argon2), override the User model’s setPasswordAttribute():
public function setPasswordAttribute($password) {
$this->attributes['password'] = Hash::make($password);
}
config/user-bundle.php for correct provider/guard names. Verify the users table has the expected columns.routes/web.php. Override or rename them if needed:
// Disable bundle routes
Route::get('/login', [AuthController::class, 'login'])->name('login');
APP_DEBUG=true) and check storage/logs/laravel.log for auth errors.Custom User Model
User model to add methods:
namespace App\Models;
use BisonLab\UserBundle\Models\User as BaseUser;
class User extends BaseUser {
public function getFullName() {
return "{$this->first_name} {$this->last_name}";
}
}
config/user-bundle.php to point to your custom model:
'model' => App\Models\User::class,
Custom Validation
User model’s rules() method or use Laravel’s FormRequest:
namespace App\Http\Requests;
use Illuminate\Validation\Rule;
class StoreUserRequest extends FormRequest {
public function rules() {
return [
'email' => ['required', Rule::unique('users')->ignore($this->user)],
];
}
}
API Resources
UserResource for API responses:
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class UserResource extends JsonResource {
public function toArray($request) {
return [
'id' => $this->id,
'email' => $this->email,
'name' => $this->name,
];
}
}
return new UserResource($user);
Testing Utilities
tests/TestCase.php:
protected function loginAsUser() {
$user = User::factory()->create();
$this->actingAs($user);
return $user;
}
How can I help you explore Laravel packages today?