diego03/path-to-regexp

This package is abandoned and no longer maintained. No replacement package was suggested.

A very simpler path-to-regexp in php

1.0.2 2024-01-25 17:58 UTC

This package is auto-updated.

Last update: 2024-03-10 21:29:30 UTC


README

The PathToRegexp PHP library allows you to easily convert route paths into regular expression patterns for flexible route matching in web applications. This readme provides an overview of the library and includes various examples of usage.

Installation

composer require diego03/path-to-regexp

Usage

Basic Usage

use Diego\PathToRegexp\PathParser;

$pathParser = new PathParser();
$pattern = $pathParser->toRegex('/users/:id/edit');

// Use $pattern in your route matching logic
if ($pattern->match($uri)) {
    // Matched!
}

Route with Parameter

$pathParser = new PathParser();
$pattern = $pathParser->toRegex('/posts/:postId');

// The resulting pattern will match paths like '/posts/123'
if ($pattern->match($uri)) {
    // Matched!
}

Route with Optional Parameter

$pathParser = new PathParser();
$pattern = $pathParser->toRegex('/articles/:slug?');

// The resulting pattern will match paths like '/articles' and '/articles/some-slug'
if ($pattern->match($uri)) {
    // Matched!
}

Route with Text Segment

$pathParser = new PathParser();
$pattern = $pathParser->toRegex('/categories/:categoryName');

// The resulting pattern will match paths like '/categories/some-category'
if ($pattern->match($uri)) {
    // Matched!
}

Complex Route

$pathParser = new PathParser();
$pattern = $pathParser->toRegex('/users/:userId/:action?');

// The resulting pattern will match paths like '/users/123' and '/users/123/edit'
if ($pattern->match($uri)) {
    // Matched!
}

Note on Regular Expression Patterns

The library uses the TRegx\CleanRegex\Pattern class for regular expression patterns. You can leverage its features for more advanced use cases. Check the T-Regx documentation for detailed information.

Contributing

Contributions are welcome! Feel free to open issues or submit pull requests.

License

This library is open-sourced software licensed under the MIT license.