Download the PHP package merophp/router without Composer

On this page you can find all versions of the php package merophp/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

Introduction

Fast, flexible and extendable request router for PHP.

Installation

Via composer:

composer require merophp/router

Basic Usage

Route definitions will be stored in route collectors. The route providers job is to provide routes by a requested HTTP method from its route collection. The router gets the routes from a provider and look for a matching route. He also executes route handlers.

require_once 'vendor/autoload.php';

use Merophp\Router\Routes\GetRoute;
use Merophp\Router\Collection\RouteCollection;
use Merophp\Router\Provider\RouteProvider;
use Merophp\Router\Router;

$collection = new RouteCollection;
$collection->addMultiple([
    new GetRoute('/api/v1/foo', function(){
        return 'Hello foo';
    }),
    new GetRoute('/api/v1/bar', function(){
        return 'Hello bar';
    }),
]);

$provider = new RouteProvider($collection);
$router = new Router($provider);

$myRoute = $router->match('GET', '/api/v1/foo');

$result = $router->dispatch($myRoute);
//Will print 'Hello bar'...
echo $result;

Route Definitions

Route definitions consists of three parts: the HTTP methods, a pattern and a handler.

use Merophp\Router\Routes\Route;

new Route(['GET','POST'], '/api/v1/foo', function(){
    echo 'I will be executed';
});

One route definition for all HTTP methods is also possible with a wildcard:

new Route(['*'], '/api/v1/foo', function(){
    echo 'I am the handler';
});

For the case that a route definition has only one HTTP method, you can also use following classes:

new GetRoute('/api/v1/foo', function(){
    echo 'I am the handler for GET:/api/v1/foo';
});
new PostRoute('/api/v1/foo', function(){
    echo 'I am the handler for POST:/api/v1/foo';
});

The Pattern

A routes pattern will be compared to a given URI path. The dispatcher will find the best fitting route definition for a given HTTP method and a URI path. You can use wildcards and placeholders in your route pattern. Values for placeholders will be passed to the routes handler.

new GetRoute('*', function(){
    echo 'I will be executed if no other definition fits';
});
new GetRoute('/api/v1/*', function(){

});
new GetRoute('/api/v1/events/{eventid}', function($eventid){
    echo $eventid;
});
new DeleteRoute('/api/v1/events/{eventid}', function($eventid){
    echo $eventid.' deleted';
});

The Handler

Any callable can be used as a route handler.

Scopes

Scopes are brackets for routes and made to prevent you from writing the same pattern parts for different routes multiple times.

use Merophp\Router\Routes\Scope;
use Merophp\Router\Routes\GetRoute;
use Merophp\Router\Routes\DeleteRoute;

new Scope('/api/v1/events/{eventid}', [
    new GetRoute('', function(){}),
    new DeleteRoute('', function(){})
]);

The pattern of the scope will be used as prefix for the patterns of the routes.


All versions of router with dependencies

PHP Build Version
Package Version
Requires php Version >=7.4
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 merophp/router contains the following files

Loading the files please wait ....