Call JavaScript From PHP & Efficiently Insert/Replace content and work with the DOM

Language

Efficiently insert/replace content and work with the DOM. It is also possible to easily call JavaScript modules from PHP.

bigpipe.svg

This library currently implements a small part of Facebook BigPipe so far, but the advantage is to efficiently insert/replace content and work with the DOM. It is also possible to easily call JavaScript modules from PHP.

Demo App

Try the app with a live demo or check how to install it.

Requirements

  • PHP 7.1 or higher
  • Node 8, 10+.
  • Webpack

Installation

These steps are required:

  1. Install composer package:
  2. $ composer require richarddobron/bigpipe 
  3. Install npm package:
  4.  $ npm install bigpipe-util 
  5. Add these lines to /path/to/resources/js/app.js:
    1.  
    2. import Primer from 'bigpipe-util/src/Primer';
    3. Primer();
    4. window.require = (modulePath) => { return modulePath.startsWith('bigpipe-util/') ? require('bigpipe-util/' + modulePath.substring(13) + '.js').default : require('./' + modulePath).default; };
  6. Create file /path/to/resources/js/ServerJS.js
    • this step is optional, but if you skip it, use this in the next step: require("bigpipe-util/ServerJS")
      1. import ServerJSImpl from 'bigpipe-util/src/ServerJS';
      2. export default
      3. class ServerJS extends ServerJSImpl { }
  7. Add these lines to the page footer:
    1.     (new (require("ServerJS"))).handle(<?=json_encode(\dobron\BigPipe\BigPipe::jsmods())?>);

DOMOPS API

  • setContent: Sets the content of an element.
  • appendContent: Insert content as the last child of the specified element.
  • prependContent: Insert content as the first child of the specified element.
  • insertAfter: Insert content after specified element.
  • insertBefore: Insert content before the specified element.
  • remove: Remove the specified element and its children.
  • replace: Replace the specified elements with content.
  • eval: Evaluates JavaScript code represented as a string.
  1.  
  2. $response = new \dobron\BigPipe\AsyncResponse();
  3. $response->setContent('div#content', $newContent);
  4. $response->send();

Refresh & Redirecting

  1.  
  2. $response = new \dobron\BigPipe\AsyncResponse();
  3. $response->reload(250); // reload page with 250ms delay
  4. // or
  5. $response->redirect('/onboarding', 500); // redirect with 500ms delay
  6. $response->send();

Payload

  1.  
  2. $response = new \dobron\BigPipe\AsyncResponse();
  3. $response->setPayload([
  4.           'username' => $_POST['username'],
  5.           'status' => 'unavailable',
  6.           'message' => 'Username is unavailable.', ]);
  7. $response->send();

BigPipe API

  • require: Call JavaScript module method. You can call a specific class method or a regular function with the custom arguments.

Example PHP code:

  1. $asyncResponse = new \dobron\BigPipe\AsyncResponse();
  2. $asyncResponse->bigPipe()->require("require('SecretModule').run()",
  3.         [ 'first argument',
  4.         'second argument',
  5.         ... ]);
  6. $asyncResponse->send();

Example JavaScript code:

class SecretModule { run(first, second) { // ... } }
  • transport: Through transport markers, you can send HTML content but also transform the content into JavaScript objects (such as Map, Set, or Element).

Example PHP code:

  1. $asyncResponse = new \dobron\BigPipe\AsyncResponse();
  2. $asyncResponse->bigPipe()->require("require('Chart').setup()", [
  3.         'element' => \dobron\BigPipe\TransportMarker::transportElement('chart-div'),
  4.         'dataPoints' => $asyncResponse->transport()->transportSet([
  5.                  ['x' => 10, 'y' => 71],
  6.                  ['x' => 20, 'y' => 55],
  7.                  ['x' => 30, 'y' => 50],
  8.                  ['x' => 40, 'y' => 65],
  9.                  ]),
  10.          ]);
  11. $asyncResponse->send();

What all can be Ajaxifed?

Links

 <a ajaxify="/ajax/remove.php" href="#" rel="async">Remove Item</a>

Forms

  1.  
  2. <form action="/submit.php" method="POST" rel="async">
  3.         <input name="user" />
  4.         <input name="Done" type="submit" />
  5. </form>
  6.  

Dialogs

 <a ajaxify="/ajax/modal.php" href="#" rel="dialog">Open Modal</a>

Inspiration

BigPipe is inspired by the concept behind Facebook's BigPipe. For more details read their blog post: Pipelining web pages for high performance.

Motivation

There is a large number of PHP projects for which moving to modern frameworks like Laravel Livewire, React, Vue.js (and many more!) could be very challenging.

The purpose of this library is to rapidly reduce the continuously repetitive code to work with the DOM and improve the communication barrier between PHP and JavaScript.

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Comments

that's exactly what I've been looking for all along time. thank you! 3

Add new comment