PHP Classes

Div PHP Ajax Mapping: Call PHP classes and functions from JavaScript

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-09-03 (2 months ago) RSS 2.0 feedStarStarStar 52%Total: 601 All time: 5,188 This week: 60Up
Version License PHP version Categories
div-ajax-mapping 1.10GNU General Publi...8.2PHP 5, Web services, AJAX
Description 

Author

This package can be used to call PHP classes and functions from JavaScript.

It can register classes or global functions to handle responses to specified requests.

The server class can then handle the requests and invoke the registered methods.

It can also register requests for specific data values that may be returned as responses to requests without registering a function for returning that data.

Innovation Award
PHP Programming Innovation award nominee
January 2015
Number 6
Many applications need to implement APIs that will be used to respond to AJAX requests performing some action.

A very common solution to implement this kind of API is to register some functions or classes that will generate the responses to the API calls.

However, in some cases the response for some types of requests is to return data already stored in variables or some other type of registered.

This class provides a simpler solution to return registered data without having to register a specific function.

Manuel Lemos
Picture of Rafa Rodriguez
  Performance   Level  
Name: Rafa Rodriguez <contact>
Classes: 7 packages by
Country: Suriname Suriname
Age: 38
All time rank: 17201 in Suriname Suriname
Week rank: 268 Up1 in Suriname Suriname Equal
Innovation award
Innovation award
Nominee: 4x

Example

<?php

declare(strict_types=1);

session_start();

use
divengine\ajaxmap;

require_once
__DIR__ . '/../src/ajaxmap.php';

// Function to get the current server time
function getServerTime(): string
{
    return
date('Y-m-d H:i:s');
}

// Encryption class with static and instance methods
class Encryption
{
    public static function
getMd5(string $value): string
   
{
        return
md5($value);
    }

    public function
getSha1(string $value): string
   
{
        return
sha1($value);
    }
}

// MyAjaxServer class extending ajaxmap
class MyAjaxServer extends ajaxmap
{
    public function
__construct(string $name)
    {
       
// Functions
       
$this->addMethod('getServerTime', false, false, [], 'Returns the current server date and time');

       
// Methods
       
$this->addMethod('getClientIP');
       
$this->addMethod('getPrivateData', false, true);
       
$this->addMethod('getProducts', false, true);

       
// Data
       
$this->addData('Date', date('D M-d \of Y'));
       
$this->addData('Server Description', 'This is an example of ajaxmap');

       
parent::__construct($name);
    }

    public function
getClientIP(): string
   
{
        return
self::getClientIPAddress();
    }

    public function
getPrivateData(): string
   
{
        return
'The number of your strong box is 53323';
    }

    public function
getProducts(): array
    {
        return [
            [
               
'Name' => 'Chai',
               
'QuantityPerUnit' => '10 boxes x 20 bags',
               
'UnitPrice' => 18,
            ],
            [
               
'Name' => 'Chang',
               
'QuantityPerUnit' => '24 - 12 oz bottles',
               
'UnitPrice' => 19,
            ],
        ];
    }
}

// Server instance
$server = new MyAjaxServer('This is an example of ajaxmap server');
$server->addClass('Encryption');
$server->go();


  Files folder image Files (12)  
File Role Description
Files folder imageexample (4 files, 1 directory)
Files folder imagesrc (2 files)
Accessible without login Plain text file .gitignore Data new
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Data Auxiliary data
Plain text file README.md Class Class source

  Files folder image Files (12)  /  example  
File Role Description
Files folder imageassets (2 files)
  Accessible without login HTML file index.html Doc. Documentation
  Accessible without login Plain text file server.php Example first commit
  Accessible without login Plain text file start.bat Data Auxiliary data
  Accessible without login Plain text file start.sh Data Auxiliary data

  Files folder image Files (12)  /  example  /  assets  
File Role Description
  Accessible without login Plain text file client.js Data Auxiliary data
  Accessible without login Plain text file style.css Data Auxiliary data

  Files folder image Files (12)  /  src  
File Role Description
  Accessible without login Plain text file ajaxmap.js Data Auxiliary data
  Plain text file ajaxmap.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:601
This week:0
All time:5,188
This week:60Up
 User Ratings  
 
 All time
Utility:66%StarStarStarStar
Consistency:58%StarStarStar
Documentation:66%StarStarStarStar
Examples:66%StarStarStarStar
Tests:-
Videos:-
Overall:52%StarStarStar
Rank:2442