PHPackages                             folded/routing - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. folded/routing

ActiveLibrary

folded/routing
==============

Routing functions for your web application.

v0.6.0(5y ago)222[4 issues](https://github.com/folded-php/routing/issues)[1 PRs](https://github.com/folded-php/routing/pulls)MITPHPPHP &gt;=7.4.0

Since Sep 5Pushed 5y ago1 watchersCompare

[ Source](https://github.com/folded-php/routing)[ Packagist](https://packagist.org/packages/folded/routing)[ RSS](/packages/folded-routing/feed)WikiDiscussions master Synced 5d ago

READMEChangelogDependencies (6)Versions (12)Used By (0)

folded/routing
==============

[](#foldedrouting)

Routing functions for your web application.

[![Packagist License](https://camo.githubusercontent.com/f5b89560fec309f1bf07882c60e2c2b3dfe5a89dc8f5ec615d98a109ab950b11/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f666f6c6465642f726f7574696e67)](https://github.com/folded-php/routing/blob/master/LICENSE) [![Packagist PHP Version Support](https://camo.githubusercontent.com/39f53657a6617dc3c75316c1365d9e408b3373245584d0f8800d1baf1f315ac6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f666f6c6465642f726f7574696e67)](https://github.com/folded-php/routing/blob/master/composer.json#L14) [![Packagist Version](https://camo.githubusercontent.com/e2cfca8e5dc9928cb7fe064bf44070dad1ac17609e206dd8a9b0a87f2265a7b9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f666f6c6465642f726f7574696e67)](https://packagist.org/packages/folded/routing) [![Build Status](https://camo.githubusercontent.com/e7fc24dce17f9de79d86f30b8d85caa67dbc52cec1e48e7f15d8e5d10a9354a7/68747470733a2f2f7472617669732d63692e636f6d2f666f6c6465642d7068702f726f7574696e672e7376673f6272616e63683d6d6173746572)](https://travis-ci.com/folded-php/routing) [![Maintainability](https://camo.githubusercontent.com/486aa0d7725ef6f6396d73d341edc0b1714304486a99bf204515a844ccd77b2e/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f30626539666261306237393930616261383134642f6d61696e7461696e6162696c697479)](https://codeclimate.com/github/folded-php/routing/maintainability) [![TODOs](https://camo.githubusercontent.com/c9d92399e61ef45c82862bc34b9bd65a07261ca2b0a58d9cbc699b266911ef19/68747470733a2f2f696d672e736869656c64732e696f2f656e64706f696e743f75726c3d68747470733a2f2f6170692e7469636b6769742e636f6d2f62616467653f7265706f3d6769746875622e636f6d2f666f6c6465642d7068702f726f7574696e67)](https://www.tickgit.com/browse?repo=github.com/folded-php/routing)

Summary
-------

[](#summary)

- [About](#about)
- [Features](#features)
- [Requirements](#requirements)
- [Installation](#installation)
- [Examples](#examples)
- [Version support](#version-support)

About
-----

[](#about)

I created this library to have a standalone, fully featured router, and be able to pull it in any project, that have some existing code or not.

Folded is a constellation of packages to help you setting up a web app easily, using ready to plug in packages.

- [folded/action](https://github.com/folded-php/action): A way to organize your controllers for your web app.
- [folded/config](https://github.com/folded-php/config): Configuration utilities for your PHP web app.
- [folded/crypt](https://github.com/folded-php/crypt): Encrypt and decrypt strings for your web app.
- [folded/exception](https://github.com/folded-php/exception): Various kind of exception to throw for your web app.
- [folded/history](https://github.com/folded-php/history): Manipulate the browser history for your web app.
- [folded/http](https://github.com/folded-php/http): HTTP utilities for your web app.
- [folded/orm](https://github.com/folded-php/orm): An ORM for you web app.
- [folded/request](https://github.com/folded-php/request): Request utilities, including a request validator, for your PHP web app.
- [folded/session](https://github.com/folded-php/session): Session functions for your web app.
- [folded/view](https://github.com/folded-php/view): View utilities for your PHP web app.

Features
--------

[](#features)

- register GET and POST routes actions (using callback)
- can match the current browsed URL and execute the associated callback
- can name the registered route to use additional functions like:
    - getting the URL from a route name

Requirements
------------

[](#requirements)

- PHP version 7.4+
- Composer installed

Installation
------------

[](#installation)

- [1. Install the package](1-install-the-package)
- [2. Bootstrap the router](#2-bootstrap-the-router)

### 1. Install the package

[](#1-install-the-package)

On your project root directory, run this command:

```
composer require folded/routing
```

### 2. Bootstrap the router

[](#2-bootstrap-the-router)

In the script that is called first, register your routes, and then call for `matchRequestedUrl()`:

```
use function Folded\addGetRoute;
use function Folded\matchRequestedUrl;

addGetRoute("/", function() {
  echo "Hello world";
});

try {
    matchRequestedUrl();
} catch (Exception $exception) {
  // ...
}
```

Examples
--------

[](#examples)

As this library is using [nikic/fast-route](https://github.com/nikic/FastRoute) internally, refer to this documentation to know [all the possibilities](https://github.com/nikic/FastRoute#defining-routes) regarding constructing the route string.

- [1. Register a GET route](#1-register-a-get-route)
- [2. Register a POST route](#2-register-a-post-route)
- [3. Catching url not found exceptions](#3-catching-url-not-found-exceptions)
- [4. Catching method not allowed exceptions](#4-catching-method-not-allowed-exceptions)
- [5. Naming a route](#5-naming-a-route)
- [6. Get the URL of a named route](#6-get-the-url-of-a-named-route)
- [7. Redirect to a named route](#7-redirect-to-a-named-route)
- [8. Redirect to an URL](#8-redirect-to-an-url)
- [9. Check if the current URL is the given one](#9-check-if-the-current-url-is-the-given-one)
- [10. Check if a route matches the current URL](#10-check-if-a-route-matches-the-current-url)

### 1. Register a GET route

[](#1-register-a-get-route)

In this example, we will register a GET route.

```
use function Folded\addGetRoute;

addGetRoute("/about-us", function() {
  echo "About us";
});
```

### 2. Register a POST route

[](#2-register-a-post-route)

In this example, we will register a POST route.

```
use function Folded\addPostRoute;

addPostRoute("/search/{search}", function($search) {
  // Pulling posts from the database...
  echo "Search result for $search";
});
```

### 3. Catching url not found exceptions

[](#3-catching-url-not-found-exceptions)

In this example, we will catch a not found exception.

```
use function Folded\matchRequestedUrl;
use Folded\Exceptions\UrlNotFoundException;

try {
  matchRequestedUrl();
} catch (Exception $exception) {
  if ($exception instanceof UrlNotFoundException) {
    // Log it, or send it to an error management system...
    // Display a 404 page...
  }
}
```

### 4. Catching method not allowed exceptions

[](#4-catching-method-not-allowed-exceptions)

In this example, we will catch a method not allowed error (which happens when you browsed an URL, but this url has been registered with another protocol).

```
use function Folded\matchRequestedUrl;
use Folded\Exceptions\MethodNotAllowedException;

try {
  matchRequestedUrl();
} catch (Exception $exception) {
  if ($exception instanceof MethodNotAllowedException) {
    // Log it, or send it to an error management system...
    // Display a 405 page...
  }
}
```

### 5. Naming a route

[](#5-naming-a-route)

In this example, we will name a route.

```
use function Folded\addGetRoute;

addGetRoute("/", function() {
  echo "welcome home";
}, "home.index");
```

### 6. Get the URL of a named route

[](#6-get-the-url-of-a-named-route)

In this example, we will get the name of a named route.

```
use function Folded\addGetRoute;
use function Folded\getRouteUrl;

addGetRoute("/user/{user}/post/{post}", function($user, $post) {
  echo "User $user posted $post";
}, "user.post.show");

echo getRouteUrl("user.post.show", ["user" => 42, "post" => 1]); // string(15) "/user/42/post/1"
```

### 7. Redirect to a named route

[](#7-redirect-to-a-named-route)

In this example, we will redirect to the URL of a named route.

```
use function Folded\addGetRoute;
use function Folded\redirectToRoute;

addGetRoute("/about-us", function() {
  echo "About us";
}, "about-us.index");

redirectToRoute("about-us.index");
```

By default, a status code `303` will be used alongside the redirection. You can override this behavior by adding the HTTP status code of your choice as second parameter:

```
use function Folded\addGetRoute;
use function Folded\redirectToRoute;

addGetRoute("/about-us", function() {
  echo "About us";
}, "about-us.index");

redirectToRoute("about-us.index", 200);
```

### 8. Redirect to an URL

[](#8-redirect-to-an-url)

In this example, we will redirect to a plain URL.

```
use function Folded\redirectToUrl;

redirectToUrl("/about-us");
```

By default, a status code `303` will be used alongside the redirection. You can override this behavior by adding the HTTP status code of your choice as second parameter:

```
use function Folded\redirectToUrl;

redirectToUrl("/", 200);
```

### 9. Check if the current URL is the given one

[](#9-check-if-the-current-url-is-the-given-one)

In this example, we will check if the current url is the one we have in parameter.

```
use function Folded\currentUrlIs;

if (currentUrlIs("/")) {
  echo "we are at the home page";
} else {
  echo "we are not at the home page";
}
```

### 10. Check if a route matches the current URL

[](#10-check-if-a-route-matches-the-current-url)

In this example, we will check if a given route name matches the URL.

```
use function Folded\currentRouteIs;
use function Folded\addGetRoute;

addGetRoute("/about-us", function() {
  echo "About us";
}, "about-us.index");

if (currentRouteIs("about-us.index")) {
  echo "this is the about us page";
} else {
  echo "this is not the about us page";
}
```

Note that if your route contains named parameter, for example `/user/{user}`, you can use the second parameter to fill the named parameter with the actual value.

```
use function Folded\currentRouteIs;
use function Folded\addGetRoute;

addGetRoute("/user/{user}", function() {
  echo "User detail";
}, "user.show");

if (currentRouteIs("user.show", ["user" => 17])) {
  echo "this is the route /user/17";
} else {
  echo "this is not the route /user/17";
}
```

Version support
---------------

[](#version-support)

7.37.48.0v0.1.0❌✔️❓v0.1.1❌✔️❓v0.1.2❌✔️❓v0.1.3❌✔️❓v0.2.0❌✔️❓v0.3.0❌✔️❓v0.4.0❌✔️❓v0.4.1❌✔️❓v0.5.0❌✔️❓v0.5.1❌✔️❓v0.6.0❌✔️❓

###  Health Score

19

—

LowBetter than 10% of packages

Maintenance0

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~2 days

Total

11

Last Release

2051d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/15908747?v=4)[Anwar](/maintainers/khalyomede)[@khalyomede](https://github.com/khalyomede)

---

Top Contributors

[![khalyomede](https://avatars.githubusercontent.com/u/15908747?v=4)](https://github.com/khalyomede "khalyomede (64 commits)")

---

Tags

phprouting

###  Code Quality

TestsPest

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/folded-routing/health.svg)

```
[![Health](https://phpackages.com/badges/folded-routing/health.svg)](https://phpackages.com/packages/folded-routing)
```

###  Alternatives

[slim/slim

Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs

12.2k49.9M1.3k](/packages/slim-slim)[workerman/webman-framework

High performance HTTP Service Framework.

149314.9k256](/packages/workerman-webman-framework)[flarum/core

Delightfully simple forum software.

211.3M1.9k](/packages/flarum-core)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
