PHP Classes

Div PHP Ways: Route HTTP requests to registered handlers

Recommend this page to a friend!
  Info   View files View files (7)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-01-17 (2 days ago) RSS 2.0 feedNot yet rated by the usersTotal: 84 This week: 4All time: 9,989 This week: 32Up
Version License PHP version Categories
div-ways 2.4.0GNU General Publi...5.4HTTP, PHP 5, Design Patterns
Description 

Author

This class can route HTTP requests to registered handlers.

It can register handler functions that will be called when the request matches given routes path patterns.

The class can also load registered scripts that define classes with functions that will handle requests to given route patterns.

Picture of Rafa Rodriguez
  Performance   Level  
Name: Rafa Rodriguez <contact>
Classes: 6 packages by
Country: Suriname Suriname
Innovation award
Innovation award
Nominee: 3x

Details

Div PHP Ways 2.4.0

A "way" is different to a "route". We need a path for found a specific resource, but we need a way for do something. This library follow this concept when implements the routing and control of PHP application.

Ways is a class that adapts the concept of SOA to the architecture of a PHP application, and tries to integrate the parts of a hybrid system.

With Ways you should think more about "control points" than on controllers of an MVC pattern. Control points are activated when they are needed, ie on demand, depending on the definition you have made.

<?php

ways::listen('sql://...', function($data, $args){
    
	ways::listen('sql://query', function($data){
	  $pdo = new PDO();
	  $st = $pdo->prepare($data['query']);
	  $st->execute($data['params']);
	  $data['result'] = $st->fetchAll(PDO::FETCH_OBJ);
	  return $data;
	});
	
});

ways::invoke('sql://query', [
    'query' => 'SELECT * FROM cats WHERE name = ?',
    'params' => ['Tom']
]);

On other platforms it is common to define all routes to the drivers in the same file and once. In Ways this is not an obligation. You can have an initial control point and depending on the URI invoked go to another control point X where routes are defined, so that the path is formed on demand, thus improving performance of its application. The structure of a URI may suggest that Ways allows a hierarchical structure of control points, but it does not, it can create a whole graph structure.

A first URI is invoked, from HTTP or CLI. But inside your code you can invoke any URIs

div ways MVC

In addition to this, a control point may require the previous execution of another control point. You can also implement events or hooks, so you can execute one control point before or after another, without the latter knowing the existence of the first. These flexibilities are valid for example in a plugins architecture.

The control points can interact, and this means, redirect the flow to another, call control points directly, exchange data and url arguments, handle the output on screen, etc.

In addition, you can establish rules for the execution of control points.

<?php

ways::rule('is-monday', function(){
    return date('D') === 'Mon';
});

ways::listen('*', function (){
    echo 'Today is Monday !!!';
}, [ways::PROPERTY_RULES => ['is-monday']]);

Ways is not only intended for the web but also for command line applications. Ways is implemented in a single class, in a single file. This allows quick start-up and easy adaptation with other platforms.

Documentation

https://github.com/divengine/ways/wiki

Installation

With composer...

composer require divengine/ways

Without composer, download the class and...

include "path/to/divengine/ways.php";

Basic usage

<?php

// arbitrary location for software's packages
define('PACKAGES', 'path/to/app/');

use divengine\ways;

// ways with closure
ways::listen("get://home", function($data){
	echo "Hello {$data['user']}";
}, "home");

// add a hook
ways::hook(DIV_WAYS_BEFORE_RUN, "home", function($data){
	$data['user'] = "Peter";
});

// listen... 
$data = ways::bootstrap('_url', 'home');

Call a static method

app/control/Home.php

<?php

#id = home
#listen = /home

class Home {
	
	static function Run()
	{
	    echo "Hello world";
	}
		
	static function About()
	{
		echo "About us";
	}
	
	#listen@Contact = get://about
	static function Contact()
	{
		echo "Contact us";
	}
}

index.php

<?php

// register a controller with the default static method ::Run()
ways::register("app/control/Home.php");

// route to another static method ([controllerID]@[method])
ways::listen("/about", "home@About");

// route to closure
ways::listen("/sayMeHello/{name}", function($data, $args) {
	echo "Hello {$args['name']}";	
});

// hook on the fly
ways::hook(DIV_WAYS_BEFORE_RUN, 
	ways::listen("/tests/...", function(){
		
		ways::listen("/tests/1", function(){
			echo "This is the test 1";
		}); 	
		
		ways::listen("/tests/2", function(){
			echo "This is the test 2";
		});
		
		if (ways::match("/tests/3")) {
			echo "This is the test 3";
		}
		
		ways::bootstrap();
	}), 
	function(){
		if (!isset($_SESSION['user']))
		{
			echo "You are not a tester";
			return false;
		}
		return true;
	});

// route to a static method
ways::bootstrap("_url", "home");

.htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((?s).*)$ index.php?_url=/$1 [QSA,L]

CLI app

<?php

// say me hello
// $ php one_script.php hello Peter
ways::listen("/hello/{name}", function ($data = [], $args = []) {
	echo "Hello {$args['name']}\n";
});

Get controller properties

<?php

$property = "This is a property value";

ways::listen("/", function ($data = [], $args = [], $properties = []) {
	echo "Controller ID = " . $properties['id'] . "\n";
	echo "A controller property = " . $properties['myProperty'];

}, [
	'myProperty' => $property,
]);

Enjoy!

--

@rafageist

Eng. Rafa Rodriguez

https://rafageist.com


  Files folder image Files  
File Role Description
Files folder imageexample (1 directory)
Files folder imagesrc (1 file)
Accessible without login Plain text file .gitignore Data Auxiliary data
Plain text file CHANGELOG.md Class Class source
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  example  
File Role Description
Files folder imagelogs (1 file)

  Files folder image Files  /  example  /  logs  
File Role Description
  Accessible without login Plain text file access.log Data Auxiliary data

  Files folder image Files  /  src  
File Role Description
  Plain text file ways.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 85%
Total:84
This week:4
All time:9,989
This week:32Up