Download the PHP package noa/router without Composer

On this page you can find all versions of the php package noa/router. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package router

Build Status codecov

Yet another Router

This a simple PHP Router handling complex route patterns

Description

Provides a simple way to handle parametrized routes, compliant PSR-7.

Installation

composer require noa/router

Usages

How to create a Router

Router handles routes, you must create a router before adding route.

<?php
require_once vendor/autoload.php

use Noa\Router\Router;

$router = new Router();

Get router instance

You can also get a Router instance through a static call, with or without custom configuration

$router = Router::getInstance();

When you want to destroy this instance to create a new one, just

Router::destroy();

How to define routes

A route is a group of three properties:

You can define routes as many as you want.

For example purpose, here is a DummyController

namespace Noa\Router\Example;

class DummyController
{
    public function testGet() {
         return 'success Get';
    }

    public function testPut() {
        return 'success Put';
    }

    public function testPost() {
        return 'success Post';
    }

    public function testDelete() {
        return 'success Delete';
    }

    public function testWithParameter($param) {

        return 'success '.$param;
    }

    public function testWithMoreParameter($param, $param2) {

        return 'success '.$param.':'.$param2;
    }

    public function testWithMoreParameterConstraint($param, $param2) {

        return 'success constraint '.$param.':'.$param2;
    }
}

Simple GET route

This the simpler route possible

$router->get('/test/closure', function (){
    return 'success closure';
});

Controller

Out of closure, you can use class method as controller

The callable must follow this pattern:

\Namespace\Of\Class\ClassName#method

Pattern matching following HTTP verb

A pattern could match multiple HTTP verb, of course you can associate the same controller to all of them.

$router->get('/object', 'Noa\Router\Example\DummyController#testGet');
$router->put('/object', 'Noa\Router\Example\DummyController#testPut');
$router->post('/object', 'Noa\Router\Example\DummyController#testPost');
$router->delete('/object', 'Noa\Router\Example\DummyController#testDelete');

Parametrized route

You can parametrized by adding a semi-colon before route part. Thus all url like:

Will match this route:

$router->get('/object/:param', 'Noa\Router\Example\DummyController#testWithParameter');

The same thing could be achieve with closure.

$router->get('/object/closure/:param', function ($param){
    return 'success closure '.$param;
});

Constraints on parameter

Sometimes you want to match a route only if parameter match a specific regex, the with method allows to add constraints on parameter (route part beginning by ":")

Those two routes have the same verb and the same pattern but are considered as different routes because constraint on :param2.

You can chain constraints as many as you want.

$router->get('/object/:id/:method/:param', 'Noa\Router\Example\DummyController#testWithMoreParameterConstraint')
    ->with('method', '[a-z]+')
    ->with('param', '[0-9]+')
    ->with('id', '[0-9]+');

$router->get('/object/:id/:method', 'Noa\Router\Example\DummyController#testWithMoreParameter');

Launch route matcher

The run method will call the route controller if the request URL and HTTP verb match with one the route

The return of run method will the controller return, feel free to do what you want with the return.

In this example we only echoing this return.

If none of route matches, an exception is raised.

try {

    echo $router->run();

} catch (\Noa\Router\RouterException $e) {

    switch ($e->getCode()) {
        case \Noa\Router\RouterException::ROUTE_NOT_FOUND:
            // Some 404 page
            break;
        default:
            // Something else
            break;
    }
}

All this code is available into example folder.

You can use PHP intern server by

cd example
php -S localhost:8082 -t .

Then you can request with curl or Postman to test route matching

PSR-7

This Router is PSR-7 compliant. It means that you receive an object Request as describe in PGP-FIG and return a Response also compliant PSR-7.

Two function are available which return the objects:

The first one is read only and handle all parameter receive by server.

$request=Router::getRequest();

The second one allow to set data into response before return.

$response=Router::getResponse();

More information about here.


All versions of router with dependencies

PHP Build Version
Package Version
Requires php Version >5.5
psr/http-message Version ^1.0
guzzlehttp/psr7 Version ^1.4
http-interop/response-sender Version ^1.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package noa/router contains the following files

Loading the files please wait ...