nojimage / phpvjs
PSR-15 Middleware for passing PHP variables to JavaScript
Requires
- php: ^7.2 || ^8.0
- ext-json: *
- psr/http-server-middleware: ^1.0 || ^2.0
Requires (Dev)
- laminas/laminas-diactoros: ^1.8 || ^2.4 || ^3.0
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^8.5 || ^9.0 || ^10.0
- squizlabs/php_codesniffer: ^3.7
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 provide 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 7.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 4.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 to Vue.js
In 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.