Download the PHP package pvettori/router without Composer

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

PHP Router: app routing for humans

Latest Version PHP Version MIT License

A simple router utility for applications.

Web applications are, in their essence, software that respond to an HTTP request.
This simple router offers a quick and easy way to define the routes of your application.

Contents

  1. Quick start
    1a. Installation
    1b. Use example
  2. Usage
  3. Advanced Usage
  4. Reference
    4a. PVproject\Routing\Middleware
    4b. PVproject\Routing\Route
    4c. PVproject\Routing\Router

Quick start

Installation

Use example

Usage

You can create a route:

Then add it to a router:

And finally run the router:

You may want to define a route with path parameters:

Note that path parameters are automatically passed as arguments with the same name to the action function (in no particular order).

Path parameters can also be injected as associative array through the $parameters argument.

Or have access to the request object in your route action (the router handles a PSR-7 Request object):

Note that any action function argument named $request gets automatically assigned the server request object.

And maybe define a path prefix for your subsequent routes:

Advanced Usage

In order to restrict a route to respond to specific methods an array of methods can be passed as additional argument.

The Route $action argument accepts the name of a class method or the name of an invokable class (a class with the magic method __invoke()).

A Route can have middleware assigned to it.
Middleware functions and classes must all accept at least two arguments, the first being the server request (modified by the previous middleware) and the second being the next handler.

Middleware may require extra arguments.
Those arguments can be passed by entering the middleware as an array with the first item being the middleware function or class and the subsequent items being the extra arguments in exact order.

Routes can also be grouped by prefix:

Extra arguments can be provided to the router.
Such arguments are automatically injected in the route action.

Reference

PVproject\Routing\Middleware

Abstract class to be extend in order to help create middleware classes.

Middleware Methods

__invoke(RequestInterface $request, callable $handler): ResponseInterface

The only required method of a Middleware class. Argument Type Description
$request RequestInterface The server request object (modified by previous middleware).
$handler callable The next middleware or route action.

PVproject\Routing\Route

A class representing a single route.
The Route object is immutable.

Route Methods

__construct(string $path, $action, [array $methods], [string $name])

Create a new Route. Argument Type Description
$path string The route path.
Path parameters can be declared with braces (ex.: "/path/{param}").
NOTE: Parameter names start with a letter or underscore, followed by any number of letters, numbers, or underscores.
Path parameters can also be restricted by appending a colon and a regex to the parameter name (ex.: "/path/{param:\d+}").
NOTE: The prameter name cannot be "this" as it would be injected as the reserved word $this.
NOTE: The regex does not accept the / character and the { , } , ^ and $ metacharacters.
$action mixed A function, function name, class method name or invokable class name that gets executed if the route matches the current server request.
$methods array Optional.
An array of HTTP request methods.
Valid methods are: "GET", "PUT", "POST", "PATCH", "DELETE", "HEAD", "OPTIONS". If not declared then the route matches any method.
$name string Optional.
A name for the route.

getAction(): callable

Returns the route action function.

getAttributes(): array

Returns the route attributes.

getMetohds(): array

Returns the route methods.

getMiddleware(): array

Returns the route middleware.

getName(): ?string

Returns the route name.

getPath(): string

Returns the declared route path.

matches(RequestInterface $request, [array &$pathParams]): bool

Check if the route matches a given request object. Argument Type Description
$request RequestInterface A request object.
&$pathParams array Optional.
An array populated with the defined path parameters.

withAttributes(array $attributes): Route

Return a new instance with an added attributes.
Attributes are passed by name as arguments to the action.
Argument Type Description
$attributes array An associative array containing attributes.
Attribute names must match this regex: ^[a-zA-Z_][a-zA-Z0-9_]*$.

withMiddleware($middleware [, $middleware] [, ...])

Returns a new Route object with middleware assigned to it. Argument Type Description
$middleware string|callable A middleware class or function.
If extra arguments need to be passed to the middleware then the definition can be expressed as an array with the first argument being the middleware class or function and the subsequent arguments being the extra arguments in exact order.

withName(string $name): Route

Returns a new Route object with the specified name. Argument Type Description
$name string The route name.

withPath(string $path): Route

Returns a new Route object with the specified path. Argument Type Description
$path string The route path.
See Route::__construct() for details.

Route Factory Methods

Route::create(string $path, $action, [array $methods])

Create a new Route.

Route::get(string $path, $action)

Create a new Route with the "GET" method.

Route::put(string $path, $action)

Create a new Route with the "PUT" method.

Route::post(string $path, $action)

Create a new Route with the "POST" method.

Route::patch(string $path, $action)

Create a new Route with the "PATCH" method.

Route::delete(string $path, $action)

Create a new Route with the "DELETE" method.

PVproject\Routing\Router

The router class.

Router Methods

__construct([array $config])

Create a new Router. Argument Type Description
$config array A configuration array.
Available configuration options: arguments, fallback, prefix.

getRoute(string $name): ?Route

Get the a named route.

getRoutes(): array

Get the defined routes.

run([array $arguments])

Run the router. Argument Type Description
$arguments array An associative array of extra arguments injected into the action function.
Arguments injected by default are: $parameters, $request, $route.

setPrefix([string $prefix]): Router

Set a route prefix. The prefix is prepended to the path of every subsequent route.
Routes delcared prior to this method are not affected.
Argument Type Description
$prefix string Optional.
The path prefix.
NOTE: calling ->setPrefix() without argument removes the prefix.

setFallback($action): Router

Set an action that gets executed when no route match is found. Argument Type Description
$action mixed The fallback action.

addRoute(Route $route): Router

Add a Route. Argument Type Description
$route Route The Route object.

addRouteGroup(string $prefix, array $routes, [array $middleware]): Router

Add multiple routes grouped by prefix.
Previously declared prefixes are prepended to the group prefix.
Subsequent routes are not affected by the group prefix.
Argument Type Description
$prefix string The grouped routes prefix.
$routes array The grouped routes.
$middleware array An array of middleware applied to all routes in the group.

run([array $arguments])

Run the route matching. Argument Type Description
$arguments array Associative array of arguments injected into the action function. Route attributes and path parameters are also injected as arguments.
Route attributes have precendence over run arguments.
Path parameters have precendence over Route attributes and run arguments.

setRoute(string $path, $action, [array $methods], [string $name]): Route

Adds a route and returns the Route object.
See Route::__construct() for details.

Router Factory Methods

Router::create([array $config])

Create a new Router. Argument Type Description
$config array See Router::__construct().

All versions of router with dependencies

PHP Build Version
Package Version
Requires guzzlehttp/guzzle Version ^6.5.8||^7.4.5
php Version >=7.1
guzzlehttp/psr7 Version ^1.9||^2.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 pvettori/router contains the following files

Loading the files please wait ....