承接 coderflexx/laravel-presenter 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

coderflexx/laravel-presenter

Composer 安装命令:

composer require coderflexx/laravel-presenter

包简介

Write Clean/Reusable Code with Presenters.

README 文档

README

The Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

A clean way to present your model attributes without putting them in the wrong file.

Installation

You can install the package via composer:

composer require coderflexx/laravel-presenter

You can publish the config file with:

php artisan vendor:publish --provider="Coderflex\LaravelPresenter\LaravelPresenterServiceProvider"

This is the contents of the published config file:

return [

    /*
    |--------------------------------------------------------------------------
    | Presenter Namespace
    |--------------------------------------------------------------------------
    |
    | This value informs LaravelPresenter which namespace you will be 
    | selecting to store your presenters by default.
    | If this value equals to null, "App\Presenter" will be used 
    | by default.
    |
    */

    'presenter_namespace'   => 'App\\Presenters',
];

Usage

The implementation of this package is so simple, all what you need to do is the following:

Model Implementation

  • Implement CanPresent Interface
  • Use UsesPresenters Trait
use Coderflex\LaravelPresenter\Concerns\CanPresent;
use Coderflex\LaravelPresenter\Concerns\UsesPresenters;
// ...

class User extends Authenticatable implements CanPresent
{
    use UsesPresenters;

    // ...
}

Create New Model Presenter class

This package gives you an easy way to generate new Presenter class, all you need to do is to use presenter:make command.

php artisan presenter:make UserPresenter

UserPresenter in our case, leaves by default in App\Presenters.

This is the contents of the UserPresenter file:

<?php

namespace App\Presenters;

use Coderflex\LaravelPresenter\Presenter;

class UserPresenter extends Presenter
{
    // 
}

If you want to change the directory, you have two options.

First options is to set the full namespace while you're creating the presenter class

php artisna presenter:make App\Models\Presenter\UserPresenter

Or change presenter_namespace from config/laravel-presenter file.

return [
    ...

    'presenter_namespace'   => 'App\\Presenters',

    ...
];

Using the Presenter Generated Class

After you create the presenter class, you need to register it on the Model by adding the $presenters protected property:

use App\Presenters\UserPresenter;
use Coderflex\LaravelPresenter\Concerns\CanPresent;
use Coderflex\LaravelPresenter\Concerns\UsesPresenters;
// ...

class User extends Authenticatable implements CanPresent
{
    use UsesPresenters;

    protected $presenters = [
        'default' => UserPresenter,
    ];
}

By default, the type of your presenter class is default, but you can use as many of presenters you want, just by identifying the type in $presenters property.

Example

Now, after we generated the presenter class, and we implemented it successfully in our model, we can use it like so:

In your UserPresenter class or any presenter class you generated.

...
class UserPresenter extends Presenter
{
    public function fullName()
    {
        return "{$this->model->first_name} {$this->model->last_name}";
    }
}
...

We add a new method to present the fullName.

In your blade or any place you want, you can do:

$user->present()->fullName

Your application will show the full name from the method you added.

Adding Another Presenter Type

Like I said above, by default the type will be default but, you can add more types as you need.

Here is an example:

use App\Presenters\UserPresenter;
use Coderflex\LaravelPresenter\Concerns\CanPresent;
use Coderflex\LaravelPresenter\Concerns\UsesPresenters;
// ...

class User extends Authenticatable implements CanPresent
{
    use UsesPresenters;

    protected $presenters = [
        'default' => UserPresenter,
        'setting' => UserSettingPresenter,
    ];
}

Generate new UserSettingPresenter

php artisan presenter:make UserSettingPresenter

Add anything to UserSettingPresenter method

...
class UserSettingPresenter extends Presenter
{
    public function lang()
    {
        return $this->model->settings->defaultLang;
    }
}
...

Finally, set setting as a type:

$user->present('setting')->lang;

By that, you can split your logic and make your code base even cleaner.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

coderflexx/laravel-presenter 适用场景与选型建议

coderflexx/laravel-presenter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 119.26k 次下载、GitHub Stars 达 31, 最近一次更新时间为 2022 年 02 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 coderflexx/laravel-presenter 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 119.26k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 32
  • 点击次数: 27
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 31
  • Watchers: 0
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-02-12