定制 alexbusu/php-js 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

alexbusu/php-js

Composer 安装命令:

composer require alexbusu/php-js

包简介

Micro-framework which translates PHP-defined actions in browser using jQuery

README 文档

README

A micro-framework for translating PHP-defined actions into browser actions using jQuery. This library allows you to send commands (we call them "triggers") from your PHP backend to your JavaScript frontend.

Installation

The recommended way to install this library is through Composer.

composer require alexbusu/php-js

Usage

PHP Backend

First, you need to create a Phpjs response object in your PHP code. You can then add triggers to this object.

<?php

require 'vendor/autoload.php';

use Alexbusu\Phpjs;

// Get the response object
$response = Phpjs::response();

// Add a message to be displayed to the user
$response->message('Your request was successful!');

// Redirect the user to a new page after 2 seconds
$response->redirect('/new-page', ['timeout' => 2]);

// Log some data to the browser console
$response->consoleLog(['some' => 'data']);

// Trigger a custom event
$response->trigger('custom.event', ['foo' => 'bar']);

// Send the response as JSON
header('Content-Type: application/json');
echo json_encode($response);

JavaScript Frontend

Include the provided JavaScript file in your HTML, along with jQuery.

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="vendor/alexbusu/php-js/src/js/jquery.setup.js"></script>

The jquery.setup.js script will automatically handle JSON responses from AJAX calls and trigger the corresponding events.

For custom triggers, you need to set up your own event handlers.

$(document).on('custom.event', function(event, data) {
    console.log('Custom event triggered!');
    console.log(data); // { foo: 'bar' }
});

Built-in Triggers

This library comes with a set of built-in triggers:

  • doc.Status: Display a message to the user. You need to implement the UI for this yourself.
  • winrd: Redirect the browser to a new URL.
  • winreload: Reload the current page.
  • error.console: Log an error message to the console.
  • warn.console: Log a warning message to the console.
  • table.console: Display tabular data in the console.
  • info.console: Log an informational message to the console.
  • log.console: Log a general message to the console.
  • set-cookie: Set a cookie in the browser.

Custom Triggers

You can define and trigger your own custom events from the PHP backend.

PHP:

$response->trigger('my.custom.trigger', '#my-element', ['some' => 'data']);

JavaScript:

$('#my-element').on('my.custom.trigger', function(event, data) {
    // Handle the event
});

Debugging

You can enable debug mode by passing ['debug' => true] to the Phpjs constructor or the response() method. When debug mode is enabled, exceptions thrown in PHP will be sent to the browser console with a full stack trace.

// Enable debug mode
$response = Phpjs::response(['debug' => true]);

// ...

// This will now include the stack trace in the response
$response->message(new \Exception('Something went wrong.'));

统计信息

  • 总下载量: 550
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-12-03

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固