# Class HttpSoft\Router\RouteCollector

A class that provides methods for creating and implementing routes.

Source code on GitHub.

use HttpSoft\Router\RouteCollector;

/**
 * @var mixed $handler
 */

$router = new RouteCollector();

$router->add('page.add', '/page/add', $handler, ['GET', 'POST']);
$router->any('page.any', '/page/any', $handler);
$router->get('post.get', '/page/get', $handler);
$router->post('post.post', '/page/post', $handler);
$router->put('post.put', '/page/put', $handler);
$router->patch('post.patch', '/page/patch', $handler);
$router->delete('post.delete', '/page/delete', $handler);
$router->head('post.head', '/page/head', $handler);
$router->options('post.options', '/page/options', $handler);

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

$router->group('/post', static function (RouteCollector $router): void {
    // '/post/post-slug'
    $router->get('post.view', '/{slug}', ViewHandler::class)->tokens(['slug' => '[\w-]+']);
    // '/post' or '/post/page-14'
    $router->get('post.list', '{[page]}', ListHandler::class)->tokens(['page' =>  'page-\d+']);
    // '/post/delete/13'
    $router->delete('post.delete', '/delete/{id}', DeleteHandler::class)->tokens(['id' => '\d+']);
    // '/post/create/11' or '/post/update/12'
    $router->add('post.edit', '/(create|update)/{id}', CreateHandler::class, ['GET', 'POST'])
        ->tokens(['id' => '\d+'])
    ;
});

The result will be equivalent to:

$router->get('post.view', '/post/{slug}', ViewHandler::class)->tokens(['slug' => '[\w-]+']);
$router->get('post.list', '/post{[page]}', ListHandler::class)->tokens(['page' =>  'page-\d+']);
$router->delete('post.delete', '/post/delete/{id}', DeleteHandler::class)->tokens(['id' => '\d+']);
$router->add('post.edit', '/post/(create|update)/{id}', CreateHandler::class, ['GET', 'POST'])
    ->tokens(['id' => '\d+'])
;

Nested groups are also supported, in which case the prefixes of all the nested groups are combined.

# Public methods

public function __construct(HttpSoft\Router\RouteCollectionInterface $routes = null);

If $routes was not specified or is null, HttpSoft\Router\RouteCollection will be used.

# routes

Gets an instance of the HttpSoft\Router\RouteCollectionInterface with all routes set.

public function routes(): HttpSoft\Router\RouteCollectionInterface;

# group

Creates a route group with a common path prefix.

/**
 * @param string $prefix common path prefix for the route group.
 * @param callable $callback callback that will add routes with a common path prefix.
 */
public function group(string $prefix, callable $callback): void;

The callback can take a HttpSoft\Router\RouteCollector instance as a parameter.

# add

Adds a route and returns it.

/**
 * @param string $name unique route name.
 * @param string $pattern path pattern with parameters.
 * @param mixed $handler action, controller, callable, closure, etc.
 * @param array $methods allowed request methods of the route.
 * @return HttpSoft\Router\Route
 */
public function add(string $name, string $pattern, $handler, array $methods): Route;

# any

Adds a generic route for any request methods and returns it.

/**
 * @param string $name unique route name.
 * @param string $pattern path pattern with parameters.
 * @param mixed $handler action, controller, callable, closure, etc.
 * @return HttpSoft\Router\Route
 */
public function any(string $name, string $pattern, $handler): Route;

# get

Adds a route only for the GET method and returns it.

/**
 * @param string $name unique route name.
 * @param string $pattern path pattern with parameters.
 * @param mixed $handler action, controller, callable, closure, etc.
 * @return HttpSoft\Router\Route
 */
public function get(string $name, string $pattern, $handler): Route;

# post

Adds a route only for the POST method and returns it.

/**
 * @param string $name unique route name.
 * @param string $pattern path pattern with parameters.
 * @param mixed $handler action, controller, callable, closure, etc.
 * @return HttpSoft\Router\Route
 */
public function post(string $name, string $pattern, $handler): Route;

# put

Adds a route only for the PUT method and returns it.

/**
 * @param string $name unique route name.
 * @param string $pattern path pattern with parameters.
 * @param mixed $handler action, controller, callable, closure, etc.
 * @return HttpSoft\Router\Route
 */
public function put(string $name, string $pattern, $handler): Route;

# patch

Adds a route only for the PATCH method and returns it.

/**
 * @param string $name unique route name.
 * @param string $pattern path pattern with parameters.
 * @param mixed $handler action, controller, callable, closure, etc.
 * @return HttpSoft\Router\Route
 */
public function patch(string $name, string $pattern, $handler): Route;

# delete

Adds a route only for the DELETE method and returns it.

/**
 * @param string $name unique route name.
 * @param string $pattern path pattern with parameters.
 * @param mixed $handler action, controller, callable, closure, etc.
 * @return HttpSoft\Router\Route
 */
public function delete(string $name, string $pattern, $handler): Route;

Adds a route only for the HEAD method and returns it.

/**
 * @param string $name unique route name.
 * @param string $pattern path pattern with parameters.
 * @param mixed $handler action, controller, callable, closure, etc.
 * @return HttpSoft\Router\Route
 */
public function head(string $name, string $pattern, $handler): Route;

# options

Adds a route only for the OPTIONS method and returns it.

/**
 * @param string $name unique route name.
 * @param string $pattern path pattern with parameters.
 * @param mixed $handler action, controller, callable, closure, etc.
 * @return HttpSoft\Router\Route
 */
public function options(string $name, string $pattern, $handler): Route;