Laravel Null Object Pattern for User fetched from Request
zendraxl/laravel-request-null-user is a Laravel package for laravel null object pattern for user fetched from request.
It currently has 12 GitHub stars and 8 downloads on Packagist (latest version 1.0.8).
Install it with composer require zendraxl/laravel-request-null-user.
Discover more Laravel packages by zendraxl
or browse all Laravel packages to compare alternatives.
Last updated
Null Object Pattern for User fetched from Request for Laravel framework.
To install just run following command from terminal:
composer require zendraxl/laravel-request-null-user
Since the user() method is already taken on \Illuminate\Http\Request object, this package provides owner() method cause it makes some sense that the person that made the request is the owner of that request.
You can use any of these:
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(\Illuminate\Http\Request $request)
{
$owner = $request->owner();
// ...
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$owner = \Illuminate\Support\Facades\Request::owner();
// ...
}
request() function/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$owner = request()->owner();
// ...
}
Object returned by the owner() method is an instance of \Zendraxl\LaravelRequestNullUser\Owner. It is a simple wrapper/decorator so \App\User class does not have to be modified.
What this is allowing for is, that it can somewhat guarantee the same API across both \App\User and \Zendraxl\LaravelRequestNullUser\NullUser objects without code duplication and respecting the DRY (Don't Repeat Yourself).
Only method defined on the \Zendraxl\LaravelRequestNullUser\Owner object is isGuest() method. This method will return true if the visitor is not authenticated, and false if the visitor is authenticated.
Out of the box \Zendraxl\LaravelRequestNullUser\NullUser comes with following public properties:
public $email = '[email protected]';
public $name = 'Guest';
public $type = 'guest';
All of them can be accessed directly through the \Zendraxl\LaravelRequestNullUser\Owner object:
echo $owner->email; // [email protected]
echo $owner->name; // Guest
echo $owner->type; // guest
Each default can be changed by defining following env variables:
ZENDRAXL_NULL_USER_EMAIL=zendraxl@gmail.com
ZENDRAXL_NULL_USER_NAME=Drazen
ZENDRAXL_NULL_USER_TYPE=alien
\Zendraxl\LaravelRequestNullUser\Owner object is extendable via Macros since it is using Illuminate\Support\Traits\Macroable trait.
Add isAdmin() method to \Zendraxl\LaravelRequestNullUser\Owner:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Zendraxl\LaravelRequestNullUser\Owner;
class AppServiceProvider extends ServiceProvider
{
public function boot(): void
{
Owner::macro('isAdmin', function () {
return $this->type === 'admin';
});
}
}
Additional isType() methods can be also added to the \Zendraxl\LaravelRequestNullUser\Owner object with env variable ZENDRAXL_USER_TYPES. By adding a comma separated list of types everything will be generated on the fly.
If the database has admin, manager, supervisor, pro user, user types on the \App\User model and this is added to the .env file:
ZENDRAXL_USER_TYPES=admin,manager,supervisor,pro user,user
then the following methods will be added to the \Zendraxl\LaravelRequestNullUser\Owner object:
$owner->isAdmin();
$owner->isManager();
$owner->isSupervisor();
$owner->isProUser();
$owner->isUser();
If the property on the \App\User model is not type but something like role, then override it with env variable
ZENDRAXL_TYPE_PROPERTY=role