PHP Classes

PHP Route Controller: Register routes and dispatch controller requests

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStar 54%Total: 394 All time: 6,625 This week: 673Up
Version License PHP version Categories
route-controller 1.0.0GNU General Publi...5.4HTTP, PHP 5, Design Patterns
Description 

Author

This class can register routes and dispatch controller requests.

It can register one or more request URL route patterns and assign closure functions to dispatch requests to those URLs.

It can also take an optional closure function to handle errors that happened during the dispatching of a request of a registered route.

The class can process the incoming HTTP request and check if it matches any of the registered routes, so it can dispatch the request to the respective closure function.

Picture of Aleksandar Zivanovic
  Performance   Level  
Name: Aleksandar Zivanovic <contact>
Classes: 16 packages by
Country: Serbia Serbia
Age: 30
All time rank: 16206 in Serbia Serbia
Week rank: 216 Up1 in Serbia Serbia Up
Innovation award
Innovation award
Nominee: 4x

Example

<?php

require_once 'RouteController.php';

/**
 * This is only way of getting instance,
 * as we want to keep all loaded routes into memory
 * we are using Singleton pattern
 */
$router = RouteController::getInstance();

/**
 * Register GET route /test/get that requires parameter name
 *
 * url to this will look like domain/index.php?route=/test/get&name=World
 *
 * output will look like this:
 *
 * GET Method.
 * Hello, World!
 * Copyright ©2015
 *
 * if parameter NAME is not defined, then second closure will be called
 *
 * output would look like:
 *
 * Missing following parameters 'name'
 *
 * Copyright ©2015
 *
 */
$router->registerRoute(RouteController::METHOD_GET, '/test/get', function ($name) {
    echo
'GET Method.<br>';
    echo
"Hello, {$name}!";
}, function (array
$missingParameters) {
    echo
'Missing following parameters \'' . implode("', '", $missingParameters) . '\'<br>';
});


/**
 * This is same as GET method, only this is post but instead of showing message
 * of missing arguments, script will throw RuntimeException
 */
$router->registerRoute(RouteController::METHOD_POST, '/test/post', function ($name) {
    echo
'POST Method.<br>';
    echo
"Hello, {$name}";
});

$router->run(function () {
   
$year = date('Y');

    echo
"<br>Copyright &copy;{$year}";
});


  Files folder image Files (2)  
File Role Description
Plain text file RouteController.php Class Class itself
Accessible without login Plain text file example.php Example How to use RouteController

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  
 0%
Total:394
This week:0
All time:6,625
This week:673Up
 User Ratings  
 
 All time
Utility:75%StarStarStarStar
Consistency:91%StarStarStarStarStar
Documentation:-
Examples:75%StarStarStarStar
Tests:-
Videos:-
Overall:54%StarStarStar
Rank:2109