hihaho/laravel-js-store
Composer 安装命令:
composer require hihaho/laravel-js-store
包简介
Laravel JS Store - Easily render data to your blade templates to use in your frontend, like Vue
关键字:
README 文档
README
Easily pass data to your view to create an initial state for your frontend. This package lets you easily create and register global data providers (for each page), which for example is useful for user data. You can also manually push data on the fly, for example in a controller.
Installation
You can install the package via composer:
composer require hihaho/laravel-js-store:^5.0
Next you should render the js data on your page, there are a few different ways to do this:
Blade directive
Add the @frontend_store directive to your view:
<html> <head> </head> <body> @frontend_store </body> </html>
Overwrite the default view
Publish the current one using php artisan vendor:publish --tag=laravel-js-store-views
or create a new view: resources/views/vendor/js-store/script.blade.php
Output the data the way you want and then include it using the blade directive (@frontend_store).
Usage
There are two methods of pushing data to the store, through data-providers or pushing manually.
Pushing manually
At any point in your application you can push data to the store using the helper, facade or through the laravel container.
You can push pretty much any type of data, as long as it can be cast to a string.
// Using the helper frontend_store()->put('user', Auth::user()); // Using the facade \HiHaHo\LaravelJsStore\StoreFacade::put('user', Auth::user()); // Using the laravel container app()->make(\HiHaHo\LaravelJsStore\Store::class)->put('user', Auth::user()); app()->make('js-store')->put('user', Auth::user()); // Using the view or response macro class Controller { public function index() { return view('index') ->js('foo', 'bar'); } public function create() { return response() ->view('create') ->js([ 'foo' => 'Fred', 'bar' => 'Waldo', ]); } }
Data-providers
Data-providers are classes that can be used to globally define data that should be send to your frontend. It's also a convenient way to store more logic related to the data, or define a rule if the data needs to be rendered.
The data-providers are defined in your config, so first you'll have to publish the config file.
php artisan vendor:publish --tag=laravel-js-store-config
This should create config/js-store.php.
Create a data provider using the artisan make command:
php artisan make:frontend-data-provider SomeName
This creates a data-provider which extends HiHaHo\LaravelJsStore\AbstractFrontendDataProvider.
An example of a data-provider might look like this:
<?php namespace App\Http\FrontendDataProviders; use HiHaHo\LaravelJsStore\AbstractFrontendDataProvider; class User extends AbstractFrontendDataProvider { /** * The data that will be JSON encoded * * @return mixed */ public function data() { return Auth::user(); } public function hasData(): bool { // Only push the data when the user is signed in return Auth::check(); } }
Next, register you data-provider in config/js-store.php:
'data-providers' => [
\App\Http\FrontendDataProviders\User::class,
],
Your data will now automatically be rendered in blade views (in this case only when the user is signed in).
Usage with Laravel Octane
This package registers a singleton to manage all data that's sent to the JS Store. Laravel Octane will register one instance per request, but only when the singleton is not accessed inside a service provider. For this reason it's not possible to push data to the store within a service provider.
Generally it won't be necessary to flush all data between requests, but if you need this behaviour you can flush the data between requests.
Any data that is pushed within a service provider won't be available in requests.
To flush the data between requests you should add the PrepareStoreForNextOperation::class listener to the following Octane events in config/octane.php:
RequestReceived::classTaskReceived::clasTickReceived::class
use HiHaHo\LaravelJsStore\Octane\PrepareStoreForNextOperation; return [ // ... 'listeners' => [ RequestReceived::class => [ // ... PrepareStoreForNextOperation::class, ], TaskReceived::class => [ // ... PrepareStoreForNextOperation::class, ], TickReceived::class => [ // ... PrepareStoreForNextOperation::class, ], ] ];
Testing
composer test
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Credits
License
The MIT License (MIT). Please see License File for more information.
Laravel Package Boilerplate
This package was generated using the Laravel Package Boilerplate.
hihaho/laravel-js-store 适用场景与选型建议
hihaho/laravel-js-store 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 69.24k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2019 年 09 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「hihaho」 「laravel-js-store」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 hihaho/laravel-js-store 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 hihaho/laravel-js-store 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 hihaho/laravel-js-store 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
统计信息
- 总下载量: 69.24k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-09-26