nojimage/phpvjs
最新稳定版本:v1.1.0
Composer 安装命令:
composer require nojimage/phpvjs
包简介
PSR-15 Middleware for passing PHP variables to JavaScript
README 文档
README
Overview
This library aims to facilitate passing values to JavaScript frameworks like Vue without the need to create an API. It is particularly useful for rapidly developing Web applications in a Multi-Page Application (MPA).
This project provides a middleware that allows passing PHP variables to JavaScript. It is designed to be used with frameworks that have a PSR-15 middleware stack.
Requirements
- PHP 8.2 or above
- Framework with PSR-15 compliant middleware stack
Installation
composer require nojimage/phpvjs
Add it to your PSR-15 middleware stack. Here's an example for CakePHP 5.x:
// src/Application.php public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue { // ... $middlewareQueue->add(new \Nojimage\PHPvJS\PHPvJSMiddleware()); // ... }
Usage
You can pass PHP variables to JavaScript using an instance of \Nojimage\PHPvJS\VariableCarry, which can be obtained from the request object attribute.
/* * @var \Psr\Http\Message\ServerRequestInterface $request */ /** @var \Nojimage\PHPvJS\VariableCarry $carry */ $carry = $request->getAttribute(\Nojimage\PHPvJS\VariableCarry::class); $carry->toJs('foo', $somevalue);
On the JavaScript side, you can access the PHP variables through the window.__phpvjs__ object.
console.log(window.__phpvjs__); // or const phpvjs = window.__phpvjs__ ?? {};
Integration with Vue.js
In the entry point script file:
import { createApp } from 'vue'; const app = createApp({ data() { // Pass PHP variables to Vue.js using PHPvJS return window.__phpvjs__ ?? {}; }, // ... other options }); // ...other setups app.mount("#app");
Contributing
If you find any bugs or have suggestions for new features, please create an issue or submit a pull request.
License
This project is released under the MIT License.
统计信息
- 总下载量: 1.15k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-06-25