Download the PHP package cklamm/router without Composer

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

There is obviously an insufficient amount of routing libraries for PHP, so I wrote this one. Jokes aside, I do not recommend to use this library instead of popular ones like FastRoute or the Symfony Routing component. I wrote it mainly as an exercise.

That being said, what can this library do? Like any router, it allows you to define which functionality your PHP application should invoke when a specific URL is requested. It also supports different HTTP methods, route parameters (including optional and wildcard parameters), route grouping, middleware and path generation through named routes.

Contents

Installation

The library can be installed via Composer.

Usage Example

This is a simplified example showing the most important features. Explanations can be found in the respective sections of this documentation.

Defining Routes

Routes can be defined with the add method.

add(string $method, string $route, mixed $handler, string $name = null): Route

There are shortcut methods for common HTTP verbs (GET, POST, PUT, PATCH, DELETE). The following two definitions are equivalent.

Route Parameters

Route definitions can contain parameters. The precedence of defined routes corresponds to the order of the table below. Therefore, unlike in many other routing libraries, the order in which routes are defined does not matter, as there is no ambiguity.

Notation Type Description
foo/bar static This route has no parameters and matches exactly foo/bar.
foo/:name required This route allows any value in the second segment, e.g. foo/bar or foo/26.
foo/?name optional This route behaves like the one above, but it also matches foo alone. An optional parameter can only be followed by optional or wildcard parameters.
foo/*name wildcard This route matches foo followed by any number of segments, e.g. foo/a/b/c, but also foo alone. A wildcard must be the last segment of a route.

The following route has one required and two optional parameters. It matches paths such as calendar/2020, calendar/2020/12 and calendar/2020/12/31.

:warning: Be aware that this library does not use regular expressions for parameters, so the above route would also match calendar/foo/bar. Any validation must be done by the implementing application.

Route Groups

Routes can be grouped with the group method.

group(string $prefix, \Closure $cb): Node

Route groups can be nested.

Middleware

Many PHP frameworks make use of middleware which provides functionality that should be executed before or after a request. Not every middleware should be executed for every request. This library makes it easy to assign middleware either globally, to a group of routes, or to a single route.

Global Middleware

Middleware that should be executed for every route can be assigned directly to the router.

Instead of calling the method repeatedly, you can pass multiple middleware names at once.

Group Middleware

Middleware that should be executed for several routes can be assigned to a route group. If you do not want the grouped routes to have a common prefix, use an empty string as prefix.

Route Middleware

Middleware that should be executed for a single route only can be assigned to that route. Note that unlike global and group middleware, route middleware only applies to the HTTP method of that route.

Dispatching

The dispatch method determines which of the previously defined routes matches a given path. Usually this method is called only once for each request.

dispatch(string $method, string $path): Result

The dispatch method returns an instance of cklamm\Router\Result, which has the following public properties.

Property Description
code The HTTP status code is 200 if a matching route was found, 404 if no route was found, 405 if a route was found for another HTTP method than the requested one (see options).
method The HTTP method that was requested.
path The path that was requested.
route The matching route or null.
name The name of the matching route or null.
handler The handler of the matching route or null.
parameters An array containing the parameter values that have been extracted from the requested path. The values are in order of the parameters as they are defined in the route.
middleware An array containing middleware for the matching route. This includes global, group and route middleware in the correct order.
options An array with the HTTP methods that are available for the requested path.

If the handler that was defined for the route is a callback function, it can be executed like this:

Path Generation

The path method can be used to generate a path for a named route. This can be very useful for providing links to different pages of the application.

path(string $route, mixed $data = []): string

If a numerical array is passed to the path method, it will fill in the parameter values in the order they are provided. If an object or an associative array is passed, the method will fill in the values according to the names of the route parameters.


All versions of router with dependencies

PHP Build Version
Package Version
Requires php Version ^7.2 || ^8.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 cklamm/router contains the following files

Loading the files please wait ....