Download the PHP package atanvarno/router without Composer

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

Atanvarno\Router

Software License Latest Version Build Status Coverage Status

A PSR-7 wrapper for FastRoute.

Requirements

PHP >= 7.0 is required, but the latest stable version of PHP is recommended.

Installation

Basic Usage

Two routers are provided: SimpleRouter and CachedRouter. These both implement the Router interface.

Instantiation

By default, the GroupCountBased FastRoute driver is used. Other drivers may be specified in the router constructor using the Router::DRIVER_* constants.

See SimpleRouter::__construct() and CachedRouter::__construct().

Defining routes

Routes can be defined by using the add() method, the addGroup() method or via constructor injection. There are also shortcut methods for every HTTP method.

add()

The $method is an uppercase HTTP method string for which a certain route should match. It is possible to specify multiple valid methods using an array. There is a Router::METHOD_* constant for each valid HTTP method.

By default the $pattern uses a syntax where {foo} specifies a placeholder with name foo and matching the regex [^/]+. To adjust the pattern the placeholder matches, you can specify a custom pattern by writing {bar:[0-9]+}.

Custom patterns for route placeholders cannot use capturing groups. For example {lang:(en|de)} is not a valid placeholder, because () is a capturing group. Instead you can use either {lang:en|de} or {lang:(?:en|de)}.

Furthermore parts of the route enclosed in [...] are considered optional, so that /foo[bar] will match both /foo and /foobar. Optional parts are only supported in a trailing position, not in the middle of a route.

The $handler parameter does not necessarily have to be a callback, it could also be a controller class name or any other kind of data you wish to associate with the route. Atanvarno\Router only tells you which handler corresponds to your request, how you interpret it is up to you.

add() implements a fluent interface, allowing multiple calls to be chained.

See Router::add().

addGroup()

You can specify routes inside of a group. All routes defined inside a group will have a common prefix.

For example, defining your routes as:

Will have the same result as:

addGroup() implements a fluent interface, allowing multiple calls to be chained.

See Router::addGroup().

Constructor injection

Route information can be injected into the Router instance constructor. This parameter accepts an array of arrays containing $method, $pattern and $handler values, like a call to add().

This allows routes to be held in a separate configuration file that returns such an array:

See SimpleRouter::__construct() and CachedRouter::__construct().

Shortcut methods

For all the valid HTTP request methods shortcut methods are available. For example:

Shortcut methods implement a fluent interface, allowing multiple calls to be chained.

See Router.

Dispatching

A request is dispatched by calling the dispatch() method. This method accepts a PSR-7 RequestInterface instance.

Note that all URI paths are normalised so that they have no trailing slash and begin with a leading slash. All user supplied patterns are likewise normalised.

dispatch() returns an array whose first element contains a status code. It is one of FastRoute\Dispatcher::NOT_FOUND, FastRoute\Dispatcher::METHOD_NOT_ALLOWED or FastRoute\Dispatcher::FOUND. For the method not allowed status the second array element contains a list of HTTP methods allowed for the supplied request.

NOTE: The HTTP specification requires that a 405 Method Not Allowed response include the Allow: header to detail available methods for the requested resource. Applications using Router should use the second array element to add this header when relaying a 405 response.

For the found status the second array element is the handler that was associated with the route and the third array element is a dictionary of placeholder names to their values. For example:

See Router::dispatch().

Caching

Instead of using SimpleRouter you can use CachedRouter.

CachedRouter requires a PSR-16 cache object as a constructor parameter.

By default, CachedRouter will take its dispatch data directly from the cache and bypass and routes defined by add() calls or constructor injection. Where no dispatch data is available (for example on the first dispatch() call or if the cache data has expired) CachedRouter will generate dispatch data from the defined routes and store it in the cache.

If your route configuration has changed and you need to update the dispatch data in the cache, call refreshCache().

See CachedRouter.

Exceptions

All exceptions thrown implement the interface RouterException.

Rather than supply array results, dispatch() can instead throw exceptions for not found and method not allowed results. MethodNotAllowedException::getAllowed() provides a list of allowed methods for the required Allow: response header.

The package contains these exceptions:

A Note on HEAD Requests

The HTTP specification requires servers to support both GET and HEAD methods:

The methods GET and HEAD MUST be supported by all general-purpose servers

To avoid forcing users to manually register HEAD routes for each resource we fallback to matching an available GET route for a given resource. Applications MAY always specify their own HEAD method route for a given resource to bypass this behavior entirely.

Full API

See API.


All versions of router with dependencies

PHP Build Version
Package Version
Requires php Version ^7.0
psr/http-message Version ^1.0.1
psr/simple-cache Version ^1.0.0
fig/http-message-util Version ^1.1.2
nikic/fast-route Version ^1.2.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 atanvarno/router contains the following files

Loading the files please wait ....