2017 © Pedro Peláez
 

library flex-router

A small, lightweight php-router for creating clean, RESTful urls. Extremely easy to setup and configure.

image

erlendellingsen/flex-router

A small, lightweight php-router for creating clean, RESTful urls. Extremely easy to setup and configure.

  • Friday, March 31, 2017
  • by erlendellingsen
  • Repository
  • 2 Watchers
  • 19 Stars
  • 9 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

flexRouter logo, (*1)

Build Status MIT Licence, (*2)

flexRouter

An extremely small and lightweight PHP-router for clean, RESTful urls. Extremely easy to setup and requires no configuration. Just plain dead simple routing., (*3)

Supports parameters, different HTTP-methods and wildcard urls., (*4)

What's Changed

As the new maintainer I tried to implement a better more robust architecture without going away from the initial goal of the router which was to work off of if based control structures., (*5)

This may change in the future as the project evolves. Below is the reworked example of how I envision you using the router in its new, unit tested, form., (*6)

Installation

composer require erlendellingsen/flex-router

Setup

URL-rewrite must be enabled on the target server. This guide consists of an example on how to set it up on Apache-servers. If you do have IIS, Nginx or other examples, then please contribute!, (*7)

Apache

Configuration, (*8)

  • Apache-module mod_rewrite must be enabled.
  • Required Apache-vhost/Directory-setting AllowOverride all

.htaccess-file, (*9)

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*)$ index.php?path=$1 [NC,L,QSA]

Non-Apache

You somehow have to enable rewriting of urls in your web server/directory to point all requests to index.php?path=x. If you have somewhat idea how, but need assistance then don't hesitate to reach out., (*10)

If you do have an example for other web servers then please consider getting in touch. (Open an issue, a fork, or just email me). Contributions are very much appreciated ☺️, (*11)

Usage

The example beneath demonstrates how to setup flexRouter., (*12)

<?php

// Load the namespaces
use FlexRouter\FlexRouter;
use FlexRouter\Utilities\FlexResolver;

// Load in composer's autoloader
require_once 'vendor/autoload.php';

// Create a new router instance
$router = new FlexRouter();

/* Register your routes

Register Method        | Request Method | Route           | Route Id                           */
$router->registerRoute('GET',           '/',              'homepage'); // Can pass single method
$router->registerRoute(['GET', 'POST'], '/test',          'test');     // Or an array of methods
$router->registerRoute('GET',           '/test/:id/post', 'param');    // You can create URL params
$router->registerRoute('GET',           '/asdf*',         'wildcard'); // Or Wildcard Params

// Create your resolver
$requestMethod = $_SERVER['REQUEST_METHOD']; // Can be attained from either globals or request object
$requestUri    = $_SERVER['REQUEST_URI'];    // Can be attained from either globals or request object
$resolver      = new FlexResolver($requestMethod, $requestUri, $router);

// Start resolving your routes in whatever way you would like,
if ($resolver->resolve('homepage')) {
    echo 'The homepage route was found';

    return;
}

if ($resolver->resolve('test')) {
    echo 'The nested test route was found';

    return;
}

if ($resolver->resolve('param')) {
    echo "The param test route was found\r\n";
    echo $resolver->access()->params('url', 'id');

    return;
}

if ($resolver->resolve('wildcard')) {
    echo 'The wildcard test route was found';

    return;
}

// This is the 404 catch all
$resolver->notFound(function () {
    echo 'Nothing found';

    return;
});

Contribution

Want to contribute? Lovely! Contributions are very much appreciated. There are a ton of things to do in this project, but I don't have the time do to it. (At least not all)., (*13)

Areas where contribution is deeply needed, (*14)

  • Writing documentation (Get in touch if you need help!)
  • Making a better readme
  • Creating tests
  • Improving exception-handling
  • Better way to handle GET/POST (Support DELETE, PUT, etc).

And probably much much more., (*15)

License

MIT Copyright Erlend Ellingsen 2017. See LICENSE., (*16)

The Versions

31/03 2017

dev-master

9999999-dev

A small, lightweight php-router for creating clean, RESTful urls. Extremely easy to setup and configure.

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Erlend Ellingsen

07/03 2017

1.0.0-stable

1.0.0.0

A small, lightweight php-router for creating clean, RESTful urls. Extremely easy to setup and configure.

  Sources   Download

MIT

The Requires

  • php ^5.3.3 || ^7.0

 

by Erlend Ellingsen