Installation:
composer require dyvelop/current-user-bundle
Ensure your Laravel project uses Symfony components (e.g., via symfony/http-foundation or symfony/security-bundle for authentication).
Service Provider Registration:
Add the bundle to your config/app.php under providers (if using Laravel 5.x) or create a custom service provider to bridge Symfony-style services:
// config/app.php
'providers' => [
// ...
Dyvelop\CurrentUserBundle\ServiceProvider::class,
],
First Use Case: Fetch the current user in a controller or service:
use Dyvelop\CurrentUserBundle\CurrentUserProvider;
class UserController extends Controller
{
protected $currentUser;
public function __construct(CurrentUserProvider $currentUser)
{
$this->currentUser = $currentUser;
}
public function show()
{
$user = $this->currentUser->getUser(); // Returns null if unauthenticated
return response()->json($user);
}
}
Service Binding:
Bind the CurrentUserProvider to Laravel’s container in a service provider:
public function register()
{
$this->app->bind('dyvelop.current_user.provider', function ($app) {
return new CurrentUserProvider($app['auth']); // Laravel's auth manager
});
}
Trait-Based Injection:
Use the CurrentUserAware trait in services/controllers:
use Dyvelop\CurrentUserBundle\CurrentUserAwareTrait;
class ArticleService
{
use CurrentUserAwareTrait;
public function createArticle()
{
$article = new Article();
$article->setAuthor($this->getCurrentUser()); // Auto-injected
// ...
}
}
Annotation Setup:
For Laravel (using Doctrine via doctrine/orm), configure the annotation driver in config/doctrine.php:
'orm' => [
'entity_managers' => [
'default' => [
'mapping_types' => [
'annotation' => 'Dyvelop\CurrentUserBundle\Doctrine\AnnotationDriver',
],
],
],
],
Entity Example:
use Doctrine\ORM\Mapping as ORM;
use Dyvelop\CurrentUserBundle\Annotation as Dyvelop;
class Post
{
/**
* @ORM\ManyToOne(targetEntity="User")
* @Dyvelop\CurrentUser
*/
private $author;
}
class CurrentUserMiddleware
{
public function handle($request, Closure $next)
{
$user = auth()->user();
app('dyvelop.current_user.provider')->setUser($user);
return $next($request);
}
}
Authentication Backend Mismatch:
SecurityContext. In Laravel, ensure the provider uses Laravel’s Auth facade or manager:
$user = auth()->user(); // Laravel
// vs.
$user = $this->security->getToken()->getUser(); // Symfony
Doctrine Annotation Driver:
AnnotationDriver. Explicitly configure it in Doctrine’s ORM setup:
$driver = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver(
[__DIR__.'/../src/Entity'],
'Dyvelop\CurrentUserBundle\Doctrine\AnnotationDriver'
);
Thread Safety:
Null User Issues:
Verify the setUser() method is called before getUser(). Add a debug log:
$provider = app('dyvelop.current_user.provider');
\Log::debug('Current user:', [$provider->getUser()]);
Annotation Not Working: Clear Doctrine’s metadata cache:
php artisan doctrine:cache:clear-metadata
Custom User Provider: Extend the base provider to add logic (e.g., guest user fallback):
class CustomUserProvider extends CurrentUserProvider
{
public function getUser()
{
$user = parent::getUser();
return $user ?: new GuestUser(); // Fallback
}
}
Event Listeners: Trigger events when the user changes (e.g., for analytics):
$provider->addListener(function ($user) {
event(new UserChanged($user));
});
Laravel-Specific Adaptations:
auth() helper:
use Illuminate\Support\Facades\Auth;
class LaravelCurrentUserTrait
{
public function getCurrentUser()
{
return Auth::user();
}
}
How can I help you explore Laravel packages today?