webfox/laravel-inertia-dataproviders
最新稳定版本:v3.0.0
Composer 安装命令:
composer require webfox/laravel-inertia-dataproviders
包简介
Data providers to encapsulate logic for Inertia views
关键字:
README 文档
README
Data providers encapsulate logic for Inertia views, keep your controllers clean and simple.
Installation
Install this package via composer:
composer require foxbytehq/laravel-inertia-dataproviders
Optionally publish the configuration file:
php artisan vendor:publish --provider="Foxbyte\InertiaDataProviders\InertiaDataProvidersServiceProvider"
We assume you've already got the Inertia adapter for Laravel installed.
What Problem Does This Package Solve?
Controllers in Laravel are meant to be slim. We have Form Requests to extract our the validation & authorization logic and our display logic is in our views, so why do we still insist on making our controllers handle fetching the data for those views?
This is especially evident in Inertia applications due to the introduction of concepts like lazy and deferred props.
Data providers extract the data composition for your Inertia views into their own classes. Inertia data providers may prove particularly useful if multiple routes or controllers within your application always needs a particular piece of data.
No more 40 line controller methods for fetching data!
Usage
Make Command
Creating a new Inertia Data Provider is easy with the make:data-provider command.
Command:
php artisan make:data-provider {name}
Arguments:
{name}: The name of the enum class to be created (e.g., OrderStatus). The command will automatically append "Enum" to the name (e.g., OrderStatusEnum).{--force}: Overwrite the data provider if it already exists.
Example Usage:
php artisan make:data-provider UserProfileDataProvider
This will generate a UserProfileDataProvider in the app/Http/DataProviders directory.
Tip
Using Laravel Idea? There's a "Create Inertia Data Provider" action available!
Using a Data Provider
Data providers take advantage of the fact that Inertia::render can accept an Arrayable.
They can also be used as discrete attributes in the data array.
use App\Models\Demo; use App\DataProviders\DemoDataProvider; class DemoController extends Controller { public function show(Demo $demo) { return Inertia::render('DemoPage', new DemoDataProvider($demo)); } public function edit(Demo $demo) { return Inertia::render('DemoPage', [ 'some' => 'data', 'more' => 'data', 'demo' => new DemoDataProvider($demo), ]); } }
What Does a Data Provider Look Like?
Data providers can live anywhere, but we'll use App/Http/DataProviders for this example.
The simplest data provider is just a class that extends DataProvider, any public methods or properties will be available to the page as data.
<?php declare(strict_types=1); namespace App\Http\DataProviders; use Inertia\LazyProp; use App\Services\InjectedDependency; use Foxbyte\InertiaDataProviders\DataProvider; class DemoDataProvider extends DataProvider { public string $someData = 'data'; public function moreData(): int { return time(); } }
A full kitchen sink data provider might look like this:
<?php declare(strict_types=1); namespace App\Http\DataProviders; use Inertia\DeferProp; use Inertia\LazyProp; use App\Services\InjectedDependency; use Foxbyte\InertiaDataProviders\DataProvider; class DemoDataProvider extends DataProvider { public function __construct( /* * All public properties are automatically available in the page * This would be available to the page as `demo` */ public Demo $demo; ) { /* * Data providers have a `staticData` property, which you can use to add any data that doesn't warrant a full * property or separate method */ $this->staticData = [ /* * This will be available to the page as `title` */ 'title' => $this->calculateTitle($demo), ]; } /* * All public methods are automatically evaluated as data and provided to the page. * ALWAYS included on first visit, OPTIONALLY included on partial reloads, ALWAYS evaluated * This would be available to the page as `someData`. * Additionally these methods are resolved through Laravel's service container, so any parameters will be automatically resolved. */ public function someData(InjectedDependency $example): array { return [ 'some' => $example->doThingWith('some'), 'more' => 'data', ]; } /* * If a method is typed to return a DeferProp, it will only be evaluated in a deferred request after the page has loaded * NEVER included on first visit, OPTIONALLY included on partial reloads, ALWAYS evaluated after the page has loaded. * Additionally the deferred callback methods are resolved through Laravel's service container, so any parameters will be automatically resolved. * @see https://inertiajs.com/deferred-props */ public function deferredExample(): DeferProp { return Inertia::defer( fn () => $this->demo->aHeavyCalculation() ); } /* * If a method is typed to return a LazyProp, it will only be evaluated when requested following inertia's rules for lazy data evaluation * NEVER included on first visit, OPTIONALLY included on partial reloads, ONLY evaluated when needed * Additionally the lazy callback methods are resolved through Laravel's service container, so any parameters will be automatically resolved. * @see https://inertiajs.com/partial-reloads#lazy-data-evaluation */ public function lazyExample(): LazyProp { return Inertia::lazy( fn (InjectedDependency $example) => $example->aHeavyCalculation($this->demo) ); } /* * If a method returns a `Closure` it will be evaluated as a lazy property. * ALWAYS included on first visit, OPTIONALLY included on partial reloads, ONLY evaluated when needed * Additionally the callback methods are resolved through Laravel's service container, so any parameters will be automatically resolved. * @see https://inertiajs.com/partial-reloads#lazy-data-evaluation */ public function quickLazyExample(): Closure { return function(InjectedDependency $example): string { return $example->formatName($this->demo->user->name); }; } /* * `protected` and `private` methods are not available to the page */ protected function calculateTitle(Demo $demo): string { return $demo->name . ' Demo'; } }
Using Multiple Data Providers
Sometimes you might find yourself wanting to return multiple DataProviders DataProvider::collection is the method for you.
Each DataProvider in the collection will be evaluated and merged into the page's data, later values from DataProviders will override earlier DataProviders.
use App\Models\Demo; use App\DataProviders\TabDataProvider; use App\DataProviders\DemoDataProvider; class DemoController extends Controller { public function show(Demo $demo) { return Inertia::render('DemoPage', DataProvider::collection( new TabDataProvider(current: 'demo'), new DemoDataProvider($demo), )); } }
You can also conditionally include DataProviders in the collection:
use App\Models\Demo; use App\DataProviders\TabDataProvider; use App\DataProviders\DemoDataProvider; use App\DataProviders\EditDemoDataProvider; use App\DataProviders\CreateVenueDataProvider; class DemoController extends Controller { public function show(Demo $demo) { return Inertia::render('DemoPage', DataProvider::collection( new TabDataProvider(current: 'demo'), new DemoDataProvider($demo), )->when($demo->has_venue, function (DataProviderCollection $collection) use($demo) { $collection->push(new CreateVenueDataProvider($demo)); }) ->unless($demo->locked, function (DataProviderCollection $collection) use($demo) { $collection->push(new EditDemoDataProvider($demo)); })); } }
Or you can use the DataProviderCollection::add method to add a DataProvider to the collection later:
use App\Models\Demo; use App\DataProviders\TabDataProvider; use App\DataProviders\DemoDataProvider; use App\DataProviders\CreateVenueDataProvider; class DemoController extends Controller { public function show(Demo $demo) { $pageData = DataProvider::collection( new TabDataProvider(current: 'demo'), new DemoDataProvider($demo), ); if($demo->has_venue) { $pageData->add(new CreateVenueDataProvider($demo)); } return Inertia::render('DemoPage', $pageData); } }
If you need to return the entire dataset as an array, for instance for use in JSON responses, you can use `toNestedArray()
use App\Models\Demo; use Illuminate\Http\Request; use App\DataProviders\TabDataProvider; use App\DataProviders\DemoDataProvider; use App\DataProviders\CreateVenueDataProvider; class DemoController extends Controller { public function show(Request $request, Demo $demo) { return (new DemoDataProvider($demo))->toNestedArray(); } }
Attribute Name Formatting
The attribute name format can be configured in the configuration file by setting the attribute_name_formatter.
The package ships with three formatters under the namespace \Foxbyte\InertiaDataProviders\AttributeNameFormatters but you are free to create your own.
AsWritten
This is the default formatter. The output attribute name will be the same as the input name.
E.g. a property named $someData and a method named more_data() will be available in the page as someData and more_data.
SnakeCase
This formatter will convert the attribute name to snake_case.
E.g. a property named $someData and a method named more_data() will be available in the page as some_data and more_data.
CamelCase
This formatter will convert the attribute name to camelCase.
E.g. a property named $someData and a method named more_data() will be available in the page as someData and moreData.
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
We welcome all contributors to the project.
License
The MIT License (MIT). Please see License File for more information.
webfox/laravel-inertia-dataproviders 适用场景与选型建议
webfox/laravel-inertia-dataproviders 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.65k 次下载、GitHub Stars 达 21, 最近一次更新时间为 2022 年 05 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「inertia」 「foxbytehq」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 webfox/laravel-inertia-dataproviders 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 webfox/laravel-inertia-dataproviders 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 webfox/laravel-inertia-dataproviders 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Supercharge your PHP8 backed enums with superpowers like localization support and fluent comparison methods.
Data providers to encapsulate logic for Inertia views
Beautiful toast notifications for Laravel + Inertia.js applications. Fluent PHP API, multi-toast support, redirect-safe, with Vue 3 and React adapters.
Supercharge your PHP8 backed enums with superpowers like localization support and fluent comparison methods.
End-to-end type-safe APIs for Laravel. Like tRPC, but for Laravel + TypeScript.
Alfabank REST API integration
统计信息
- 总下载量: 5.65k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 21
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-05-04