Getting Started

Author Latest Version Software License Build Status Coverage Status Quality Score Total Downloads

What is Route?

Route is a fast PSR-7 routing/dispatcher package including PSR-15 middleware implementation that enabables you to build well designed performant web apps.

At its core is Nikita Popov’s FastRoute package allowing this package to concentrate on the dispatch of your controllers.

Route on Packagist

What isn’t Route?

Route is not a framework, it will not allow you to build an application out of the box. See getting-started for more information.

Goals

  • To provide a “friendlier” API on top of FastRoute.
  • To provide an easy interface to implement PSR-7 HTTP messages in to your applications.
  • To enable you to implement PSR-15 middleware in to your applications.
  • To provide convenience in building web applications and APIs.

Questions?

Route was created by Phil Bennett. Find him on Twitter at @philipobenito.

Installation

System Requirements

You need PHP >= 7.1.0 to use League\Route but the latest stable version of PHP is recommended.

You will also require an implementation of PSR-7 HTTP Message. Throughout the documentation we will be using Zend\Diactoros, however, there are many implementations to choose from on Packagist.

Composer

Route is available on Packagist and can be installed using Composer:

composer require league/route

Most modern frameworks will include Composer out of the box, but ensure the following file is included:

<?php

// include the Composer autoloader
require 'vendor/autoload.php';

Going Solo

You can also use Route without using Composer by registering an autoloader function:

spl_autoload_register(function ($class) {
    $prefix = 'League\\Route\\';
    $base_dir = __DIR__ . '/src/';
    $len = strlen($prefix);
    if (strncmp($prefix, $class, $len) !== 0) {
        // no, move to the next registered autoloader
        return;
    }
    $relative_class = substr($class, $len);
    $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
    if (file_exists($file)) {
        require $file;
    }
});

Or, use any other PSR-4 compatible autoloader.