2017 © Pedro Peláez
 

library via

Simple PHP Router, with prefix groups and filtering

image

alejoluc/via

Simple PHP Router, with prefix groups and filtering

  • Thursday, June 28, 2018
  • by alejoluc
  • Repository
  • 1 Watchers
  • 0 Stars
  • 3 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 0 % Grown

The README.md

alejoluc\Via

Simple PHP router with prefix grouping and filters, (*1)

Work in Progress

This README file is a work in progress and is in the process of being written. In the meantime, please refer to the examples folder to get started., (*2)

Usage

Please refer to the examples folder to see how to use the router, along with usage of grouping and filters., (*3)

Installation

From the command line:, (*4)

composer require alejoluc/via, (*5)

Or write manually in composer.json:, (*6)

{
  "require": {
    "alejoluc/via": "*"
  }
}

Using the "Facade"

A "Facade" is provided for those that want clearer code and don't mind using static classes here and there. You can use any method of the Router class when calling the Facade., (*7)

<?php
use alejoluc\Via\RouterFacade as Router;

// [Set up Router (see file in examples) 

Router::get('login', ['LoginController', 'showForm']);
Router::group('admin/', function(){
    Router::get('createUser', ['UsersController', 'createForm']);
}, ['isLoggedIn']);

Match Handlers

There are three possible scenarios when calling the dispatch() method: no route will match, or a route will match but some or all filters will fail, or a route will match and no filter fails., (*8)

In all cases, a Match object is passed to whatever match handler or handlers you have set up. You can see the src/SampleHandlers/ folder for several comprehensive handlers., (*9)

Letting the router build the request string on it's own or using setRequestString()

If no request string is manually specified, the router will try to get it automatically from $_SERVER['REQUEST_URI'] or $_SERVER['PATH_INFO']. Aditionally, it will truncate the script file name from the request string, if it is there, and it will also truncate the query string (anything after ? or &), if it exists. This will not happen if you manually set the request string via Router::setRequestString(), (*10)

As a general rule, you should manually call Router::setRequestString() only when you know what you are doing and why, and let the router do the dirty work the rest of the time., (*11)

For example, if you have no url rewriting in place in your server, you might still want to use the router and you can do it by placing the routes in a query variable, like q. An example for that approach, assuming the file name is index.php, would be:, (*12)

<?php
use alejoluc\Router\Router;
$router = new Router();
$q = isset($_GET['q']) ? $_GET['q'] : '/';
$router->setRequestString($q);

$router->get('/about', 'About Page'); // Will respond to /index.php?q=/about 

The Versions

28/06 2018

dev-master

9999999-dev https://github.com/alejoluc/Via

Simple PHP Router, with prefix groups and filtering

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

by Alejo Lucangeli

routing router

28/06 2018

0.12.2

0.12.2.0 https://github.com/alejoluc/Via

Simple PHP Router, with prefix groups and filtering

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

by Alejo Lucangeli

routing router

17/01 2017

0.5.0

0.5.0.0

PHP Router

  Sources   Download

by Alejo Lucangeli

20/06 2016

0.2.0

0.2.0.0

PHP Router

  Sources   Download

by Alejo Lucangeli

19/06 2016

0.1.1

0.1.1.0

PHP Router

  Sources   Download

by Alejo Lucangeli

19/06 2016

0.1.0

0.1.0.0

PHP Router

  Sources   Download

by Alejo Lucangeli