zendraxl/laravel-request-null-user 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

zendraxl/laravel-request-null-user

Composer 安装命令:

composer require zendraxl/laravel-request-null-user

包简介

Laravel Null Object Pattern for User fetched from Request

README 文档

README

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

Install

To install just run following command from terminal:

composer require zendraxl/laravel-request-null-user

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

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

\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

zendraxl/laravel-request-null-user 适用场景与选型建议

zendraxl/laravel-request-null-user 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8 次下载、GitHub Stars 达 12, 最近一次更新时间为 2019 年 07 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「null object」 「null user」 「request null user」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 zendraxl/laravel-request-null-user 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 zendraxl/laravel-request-null-user 我们能提供哪些服务?
定制开发 / 二次开发

基于 zendraxl/laravel-request-null-user 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 8
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 12
  • 点击次数: 14
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 12
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-07-08