PHP Classes
elePHPant
Icontem

Div PHP Ways: Route HTTP requests to registered handlers

Recommend this page to a friend!
  Info   View files Documentation   View files View files (32)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2017-12-06 (5 days ago) RSS 2.0 feedNot yet rated by the usersTotal: 49 This week: 13All time: 9,043 This week: 73Up
Version License PHP version Categories
div-ways 1.3GNU General Publi...5HTTP, PHP 5, Design Patterns
Collaborate with this project Author

divengine.div-ways - github.com

Description

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.

  Performance   Level  
Name: Rafael Rodriguez <contact>
Classes: 4 packages by
Country: Cuba Cuba
Age: 31
All time rank: 19104 in Cuba Cuba
Week rank: 37 Up1 in Cuba Cuba Up
Innovation award
Innovation award
Nominee: 2x

Details

?# Div PHP Ways 1.3

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.

With Div 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.

On other platforms it is common to define all routes to the drivers in the same file and once. In Div Ways this is not an obligation. As you can see in the examples, you can have an initial control point and depending on the URL 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 URL may suggest that Div Ways allows a hierarchical structure of contorl points, but it does not, it can create a whole graph structure.

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.

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

Basic usage

<?php

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

// include the library
include "path/to/divWays.php";

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

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

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

Call a static method

app/control/Home.php

<?php

#id = home
#listen = /home

class Home {
	
	static function Run($data)
	{
	    echo "Hello world";
	}
	
	static function About($data)
	{
		echo "About us";
	}
}

index.php

<?php

include "divWays.php";

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

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

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

.htaccess

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

See the example.

  Files folder image Files  
File Role Description
Files folder imageexample (2 files, 3 directories)
Accessible without login Plain text file .gitignore Data Auxiliary data
Accessible without login Plain text file CHANGELOG.txt Doc. Documentation
Accessible without login Plain text file composer.json Data Auxiliary data
Plain text file divWays.php Class Class source
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 imageapp (4 directories)
Files folder imagelogs (1 file)
Files folder imagepublic (2 files, 1 directory)
  Accessible without login Plain text file app.ini Data Auxiliary data
  Accessible without login Plain text file start-web-server.bat Data Auxiliary data

  Files folder image Files  /  example  /  app  
File Role Description
Files folder imageadmin (1 file, 2 directories)
Files folder imageapi (1 file)
Files folder imagescripts (2 files)
Files folder imagesite (1 file, 3 directories)

  Files folder image Files  /  example  /  app  /  admin  
File Role Description
Files folder imagecontrol (2 files)
Files folder imageviews (4 files)
  Accessible without login Plain text file index.php Aux. Auxiliary script

  Files folder image Files  /  example  /  app  /  admin  /  control  
File Role Description
  Accessible without login Plain text file Dashboard.php Aux. Auxiliary script
  Plain text file Session.php Class Class source

  Files folder image Files  /  example  /  app  /  admin  /  views  
File Role Description
  Accessible without login HTML file head.html Doc. Documentation
  Accessible without login HTML file header.html Doc. Documentation
  Accessible without login HTML file index.html Doc. Documentation
  Accessible without login HTML file login.html Doc. Documentation

  Files folder image Files  /  example  /  app  /  api  
File Role Description
  Accessible without login Plain text file bootstrap.php Aux. Auxiliary script

  Files folder image Files  /  example  /  app  /  scripts  
File Role Description
  Accessible without login Plain text file decorator.php Aux. Auxiliary script
  Accessible without login Plain text file log.php Aux. Auxiliary script

  Files folder image Files  /  example  /  app  /  site  
File Role Description
Files folder imagecontrol (5 files)
Files folder imagemodel (1 file)
Files folder imageviews (3 files)
  Accessible without login Plain text file index.php Aux. Auxiliary script

  Files folder image Files  /  example  /  app  /  site  /  control  
File Role Description
  Plain text file Blog.php Class Class source
  Plain text file Home.php Class Class source
  Plain text file News.php Class Class source
  Plain text file Prepare.php Class Class source
  Accessible without login Plain text file Tests.php Aux. Auxiliary script

  Files folder image Files  /  example  /  app  /  site  /  model  
File Role Description
  Plain text file Texts.php Class Class source

  Files folder image Files  /  example  /  app  /  site  /  views  
File Role Description
  Accessible without login HTML file header.html Doc. Documentation
  Accessible without login HTML file index.html Doc. Documentation
  Accessible without login Plain text file test-match.phtml Data Auxiliary data

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

  Files folder image Files  /  example  /  public  
File Role Description
Files folder imageassets (1 file)
  Accessible without login Plain text file .htaccess Data Auxiliary data
  Accessible without login Plain text file index.php Aux. Auxiliary script

  Files folder image Files  /  example  /  public  /  assets  
File Role Description
  Accessible without login Plain text file style.css Data Auxiliary data

 Version Control Unique User Downloads Download Rankings  
 96%
Total:49
This week:13
All time:9,043
This week:73Up