PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Aleksandar Zivanovic  >  PHP Route Controller  >  example.php  >  Download  
File: example.php
Role: Example script
Content type: text/plain
Description: How to use RouteController
Class: PHP Route Controller
Register routes and dispatch controller requests
Author: By
Last change:
Date: 6 years ago
Size: 1,332 bytes
 

Contents

Class file image Download
<?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}";
});

For more information send a message to info at phpclasses dot org.