PHPackages                             zendraxl/laravel-request-null-user - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. zendraxl/laravel-request-null-user

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

zendraxl/laravel-request-null-user
==================================

Laravel Null Object Pattern for User fetched from Request

1.0.8(6y ago)1281MITPHPPHP &gt;=7.1.0

Since Jul 8Pushed 6y agoCompare

[ Source](https://github.com/zendraxl/laravel-request-null-user)[ Packagist](https://packagist.org/packages/zendraxl/laravel-request-null-user)[ RSS](/packages/zendraxl-laravel-request-null-user/feed)WikiDiscussions master Synced 2w ago

READMEChangelog (3)Dependencies (1)Versions (9)Used By (0)

Laravel Request Null User
=========================

[](#laravel-request-null-user)

Null Object Pattern for User fetched from Request for Laravel framework.

Install
-------

[](#install)

To install just run following command from terminal:

`composer require zendraxl/laravel-request-null-user`

Usage
-----

[](#usage)

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:

- Dependency Injection Request object

```
/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index(\Illuminate\Http\Request $request)
{
    $owner = $request->owner();
    // ...
}
```

- Laravel Facade Request object

```
/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    $owner = \Illuminate\Support\Facades\Request::owner();
    // ...
}
```

- Global `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.

Defaults
--------

[](#defaults)

Out of the box `\Zendraxl\LaravelRequestNullUser\NullUser` comes with following public properties:

```
public $email = 'guest@example.com';
public $name = 'Guest';
public $type = 'guest';
```

All of them can be accessed directly through the `\Zendraxl\LaravelRequestNullUser\Owner` object:

```
echo $owner->email; // guest@example.com
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

```

Extend
------

[](#extend)

`\Zendraxl\LaravelRequestNullUser\Owner` object is extendable via `Macros` since it is using `Illuminate\Support\Traits\Macroable` trait.

Add `isAdmin()` method to `\Zendraxl\LaravelRequestNullUser\Owner`:

```
