avto-dev/back2front-laravel
Composer 安装命令:
composer require avto-dev/back2front-laravel
包简介
Laravel package for sending data from backend to frontend over JavaScript object
README 文档
README
Send backend data to frontend for Laravel applications
Package for sending data from backend to frontend JS variable.
Package a repository of the form "key" => "value" and methods for converting data to array and JSON.
Install
Require this package with composer using the following command:
$ composer require avto-dev/back2front-laravel "^2.0"
Installed
composeris required (how to install composer).
You need to fix the major version of package.
For publish config and assets execute in console next command:
$ php artisan vendor:publish --provider="AvtoDev\\Back2Front\\ServiceProvider" --force
This command will publish files ./config/back-to-front.php with basic setting for package and public/vendor/back-to-front/front-stack.js with JavaScript object for getting access to the data.
Usage
At backend
To get the stack object at backend you can use global helper:
<?php backToFrontStack();
or getting object from service container:
<?php use AvtoDev\Back2Front\Back2FrontInterface; /** @var Back2FrontInterface $service */ $service = resolve(Back2FrontInterface::class);
Methods
Back2Front object provides the following public methods:
| Method | Description |
|---|---|
put($key, $value) |
Set variable in stack. Parameter key must be a string |
get($key, [default]): mixed |
Get value by key |
has($key): bool |
Check that key exists in storage |
forget($key) |
Remove item from storage |
toArray(): array |
Return data in array |
toJson(): string |
Return data in JSON encoded |
Also you can iterate object.
Back2Front supports dot notation in put, get, has and forget methods.
<?php backToFrontStack()->put('user.name', 'John Doe');
At frontend will object:
{
"user": {
"name": "John Doe"
}
}
At frontend
For output data at frontend you should add following code in your blade-template (preferably in the section head of the resulting HTML document):
<script type="text/javascript"> Object.defineProperty(window, 'DATA_PROPERTY_NAME', { writable: false, value: {!! backToFrontStack()->toJson() !!} }); </script>
Or by blade-directive
@back_to_front_data('DATA_PROPERTY_NAME')
It creates property with name equals DATA_PROPERTY_NAME for superg lobal object window with early added data.
Default value of DATA_PROPERTY_NAME is 'backend'. If you use custom value and want to use front-stack helper on frontend, than you need call window.frontStack.setStackName('custom_name'); before helper usage.
Package contains javaScript helper for access to data object.
Use it you may adding js file at page:
<script src="/vendor/back-to-front/front-stack.js" type="text/javascript"></script>
You also can use it as require.js dependency.
This creates window.frontStack object which provides following methods:
| Method | Description |
|---|---|
get(key, [default]) |
Get value by key. Supports "dot" notation for access to items if in data contains multidimensional arrays. Returns undefined if item don't exists or default value if it set |
has(key): bool |
Check that key exists in storage |
all(): object |
Returns data object |
Example
At backend:
backToFrontStack()->put('user_id', $user->id);
At frontend:
<script type="text/javascript"> console.log(window.frontStack.get('user_id')); </script>
Testing
For package testing we use phpunit framework and docker with compose plugin as develop environment. So, just write into your terminal after repository cloning:
$ make build $ make latest # or 'make lowest' $ make test
For testing JavaScript code using Mocha and Chai framework.
Run in console npm test. Coverage report will in coverage/coverage.json and in coverage/lcov-report/index.html for humans.
Changes log
Changes log can be found here.
Support
If you will find any package errors, please, make an issue in current repository.
License
This is open-sourced software licensed under the MIT License.
avto-dev/back2front-laravel 适用场景与选型建议
avto-dev/back2front-laravel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11.96k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2018 年 10 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「backend」 「laravel」 「frontend」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 avto-dev/back2front-laravel 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 avto-dev/back2front-laravel 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 avto-dev/back2front-laravel 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Query filtering in your frontend
This bundle offers a simple backend explanation form field (inputType).
Preview of settings in footer of tt_content element in page module.
Article CSS-ID Frontend Output Optimization
An interactive tour through the TYPO3 backend.
Frontend support for the MultiColumnWizard
统计信息
- 总下载量: 11.96k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-10-12