2017 © Pedro Peláez
 

library routing

The small UniKado URL routing library.

image

uk/routing

The small UniKado URL routing library.

  • Thursday, July 28, 2016
  • by UK
  • Repository
  • 1 Watchers
  • 0 Stars
  • 6 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

UK.Routing

Simple URL routing library., (*1)

Installation

Install it via composer!, (*2)

composer require uk/routing

or include it inside you're composer.json, (*3)

{
   "require": {
      "php": ">=7.0",
      "uk/routing": "^0.1.3"
   }
}

Preparing the web server

For Router usage you need to tell the web server, that it should rewrite requests to not existing URL paths to the handling PHP script., (*4)

You can do it by 2 different ways:, (*5)

  • Passing the not existing URL path as GET variable with specific name, to the script
  • Passing the not existing URL path VIA $_SERVER[ 'REQUEST_URI' ] (best choice)

Apache web server

For apache its really simple to handle the rewrites., (*6)

Create an file with the name .htaccess and put it to the folder where the rewriting should work., (*7)

But remember!, (*8)

.htaccess (distributed configuration files) should only be used if you do'nt have access to the server configuration files., (*9)

.htaccess usage comes with some overhead which can be avoided., (*10)

But if you admit an server, there is no need to shown you more. You have to know it :-), (*11)

The contents of this file depends to the Router type that should be used., (*12)

As GET variable (RouterType::REWRITE_TO_GET)

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

The first line enables the rewrite engine. Second line declares the condition that matches all not existing file calls and the third line matches all not existing directory calls., (*13)

The last line rewrites the matching calls to not existing files and directories to index.php and passes the called, not existing URL path to the theURL GET variable, (*14)

As 'REQUEST_URI' value (RouterType::REWRITE_TO_REQUEST_URI)

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]

The first line enables the rewrite engine. Second line declares the condition that matches all not existing file calls and the third line matches all not existing directory calls., (*15)

The last line rewrites the matching calls to not existing files and directories to index.php and the called. not existing URL path is passed to $_SERVER[ 'REQUEST_URI' ], (*16)

On errors

If the .htaccess usage triggers some server errors (e.g. error 50* or something else) you have to check:, (*17)

  • if apache is enabled to use .htaccess files
  • if apache is enabled to use rewriting by .htaccess files (Rights for .htaccess stuff)
  • if apache is enabled to use the mod_rewrite extension

The first 2 things can be handled by ensure the AllowOverwrite directive. For details see: Apache HTTP Server Tutorial: .htaccess files, (*18)

AllowOverride FileInfo

To check if mod rewrite is enable call this inside you're console, (*19)

sudo a2enmod rewrite

If already enabled it outputs: Module rewrite already enabled, (*20)

If not enabled, the rewrite module gets enabled., (*21)

If you not have access to call a console at the server contact the admin or provider and ask him if mod_rewrite is enable for you. If not tell him to enable the mod_rewrite usage via .htaccess, (*22)

NGINX web server

This is a big TODO! :-), (*23)

but i think if you use:, (*24)

try_files $uri $uri/ /index.php?$args;

…it should work to get the called URL path via $_SERVER[ 'REQUEST_URI' ] inside index.php, (*25)

Usage

// Include the autoloader, created by composer
require __DIR__ . '/vendor/autoload.php';

use UK\Routing\Router;
use UK\Routing\RouterType;

// Init the router
$router = new Router(
   // The type of the router
   RouterType::REWRITE_TO_REQUEST_URI
);

// Adds an dynamic regex URI path route
$router->addRoute(
   '~^/foo/(\d+)/bar/?$~',
   function ( array $matches )
   {
      // If you need access to current Router instance can get it by Router::GetInstance()
      echo 'ID: ', $matches[ 1 ], ' OK :-)';
      exit;
   }
);

// Adds an static URI path route
$router->addRoute(
   '~^/baz/?$~',
   function ()
   {
      // If you need access to current Router instance can get it by Router::GetInstance()
      echo 'The BAZ is called!';
      exit;
   }
);

After defining you're routes you only should call execute() and the routes will be executed., (*26)

if ( ! $router->execute() )
{
   // Showing 404 error because no router matches the defined request URI path.
}

The Versions

28/07 2016

dev-master

9999999-dev

The small UniKado URL routing library.

  Sources   Download

LGPLv3

The Requires

 

by Avatar UniKado

28/07 2016

0.1.3

0.1.3.0

The small UniKado URL routing library.

  Sources   Download

LGPLv3

The Requires

 

by Avatar UniKado

26/07 2016

0.1.2

0.1.2.0

The small UniKado URL routing library.

  Sources   Download

LGPLv3

The Requires

 

by Avatar UniKado

13/07 2016

0.1.1

0.1.1.0

The small UniKado URL routing library.

  Sources   Download

LGPLv3

The Requires

 

by Avatar UniKado

12/07 2016

0.1.0

0.1.0.0

The small UniKado URL routing library.

  Sources   Download

LGPLv3

The Requires

 

by Avatar UniKado