{"id":36998508,"url":"https://github.com/folded-php/routing","last_synced_at":"2026-01-13T23:59:57.106Z","repository":{"id":62506142,"uuid":"292939376","full_name":"folded-php/routing","owner":"folded-php","description":"Routing functions for your PHP web app.","archived":false,"fork":false,"pushed_at":"2020-10-17T07:53:18.000Z","size":80,"stargazers_count":2,"open_issues_count":5,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-23T20:17:41.173Z","etag":null,"topics":["php","routing"],"latest_commit_sha":null,"homepage":"https://packagist.org/packages/folded/routing","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/folded-php.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-09-04T20:21:06.000Z","updated_at":"2021-03-02T12:30:46.000Z","dependencies_parsed_at":"2022-11-02T12:45:41.553Z","dependency_job_id":null,"html_url":"https://github.com/folded-php/routing","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"purl":"pkg:github/folded-php/routing","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/folded-php%2Frouting","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/folded-php%2Frouting/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/folded-php%2Frouting/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/folded-php%2Frouting/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/folded-php","download_url":"https://codeload.github.com/folded-php/routing/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/folded-php%2Frouting/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28406386,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-13T21:51:37.118Z","status":"ssl_error","status_checked_at":"2026-01-13T21:45:14.585Z","response_time":56,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["php","routing"],"created_at":"2026-01-13T23:59:56.540Z","updated_at":"2026-01-13T23:59:57.101Z","avatar_url":"https://github.com/folded-php.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# folded/routing\n\nRouting functions for your web application.\n\n[![Packagist License](https://img.shields.io/packagist/l/folded/routing)](https://github.com/folded-php/routing/blob/master/LICENSE) [![Packagist PHP Version Support](https://img.shields.io/packagist/php-v/folded/routing)](https://github.com/folded-php/routing/blob/master/composer.json#L14) [![Packagist Version](https://img.shields.io/packagist/v/folded/routing)](https://packagist.org/packages/folded/routing) [![Build Status](https://travis-ci.com/folded-php/routing.svg?branch=master)](https://travis-ci.com/folded-php/routing) [![Maintainability](https://api.codeclimate.com/v1/badges/0be9fba0b7990aba814d/maintainability)](https://codeclimate.com/github/folded-php/routing/maintainability) [![TODOs](https://img.shields.io/endpoint?url=https://api.tickgit.com/badge?repo=github.com/folded-php/routing)](https://www.tickgit.com/browse?repo=github.com/folded-php/routing)\n\n## Summary\n\n- [About](#about)\n- [Features](#features)\n- [Requirements](#requirements)\n- [Installation](#installation)\n- [Examples](#examples)\n- [Version support](#version-support)\n\n## About\n\nI 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.\n\nFolded is a constellation of packages to help you setting up a web app easily, using ready to plug in packages.\n\n- [folded/action](https://github.com/folded-php/action): A way to organize your controllers for your web app.\n- [folded/config](https://github.com/folded-php/config): Configuration utilities for your PHP web app.\n- [folded/crypt](https://github.com/folded-php/crypt): Encrypt and decrypt strings for your web app.\n- [folded/exception](https://github.com/folded-php/exception): Various kind of exception to throw for your web app.\n- [folded/history](https://github.com/folded-php/history): Manipulate the browser history for your web app.\n- [folded/http](https://github.com/folded-php/http): HTTP utilities for your web app.\n- [folded/orm](https://github.com/folded-php/orm): An ORM for you web app.\n- [folded/request](https://github.com/folded-php/request): Request utilities, including a request validator, for your PHP web app.\n- [folded/session](https://github.com/folded-php/session): Session functions for your web app.\n- [folded/view](https://github.com/folded-php/view): View utilities for your PHP web app.\n\n## Features\n\n- register GET and POST routes actions (using callback)\n- can match the current browsed URL and execute the associated callback\n- can name the registered route to use additional functions like:\n  - getting the URL from a route name\n\n## Requirements\n\n- PHP version 7.4+\n- Composer installed\n\n## Installation\n\n- [1. Install the package](1-install-the-package)\n- [2. Bootstrap the router](#2-bootstrap-the-router)\n\n### 1. Install the package\n\nOn your project root directory, run this command:\n\n```bash\ncomposer require folded/routing\n```\n\n### 2. Bootstrap the router\n\nIn the script that is called first, register your routes, and then call for `matchRequestedUrl()`:\n\n```php\nuse function Folded\\addGetRoute;\nuse function Folded\\matchRequestedUrl;\n\naddGetRoute(\"/\", function() {\n  echo \"Hello world\";\n});\n\ntry {\n    matchRequestedUrl();\n} catch (Exception $exception) {\n  // ...\n}\n```\n\n## Examples\n\nAs 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.\n\n- [1. Register a GET route](#1-register-a-get-route)\n- [2. Register a POST route](#2-register-a-post-route)\n- [3. Catching url not found exceptions](#3-catching-url-not-found-exceptions)\n- [4. Catching method not allowed exceptions](#4-catching-method-not-allowed-exceptions)\n- [5. Naming a route](#5-naming-a-route)\n- [6. Get the URL of a named route](#6-get-the-url-of-a-named-route)\n- [7. Redirect to a named route](#7-redirect-to-a-named-route)\n- [8. Redirect to an URL](#8-redirect-to-an-url)\n- [9. Check if the current URL is the given one](#9-check-if-the-current-url-is-the-given-one)\n- [10. Check if a route matches the current URL](#10-check-if-a-route-matches-the-current-url)\n\n### 1. Register a GET route\n\nIn this example, we will register a GET route.\n\n```php\nuse function Folded\\addGetRoute;\n\naddGetRoute(\"/about-us\", function() {\n  echo \"\u003ch1\u003eAbout us\u003c/h1\u003e\";\n});\n```\n\n### 2. Register a POST route\n\nIn this example, we will register a POST route.\n\n```php\nuse function Folded\\addPostRoute;\n\naddPostRoute(\"/search/{search}\", function($search) {\n  // Pulling posts from the database...\n  echo \"\u003ch1\u003eSearch result for $search\u003c/h1\u003e\";\n});\n```\n\n### 3. Catching url not found exceptions\n\nIn this example, we will catch a not found exception.\n\n```php\nuse function Folded\\matchRequestedUrl;\nuse Folded\\Exceptions\\UrlNotFoundException;\n\ntry {\n  matchRequestedUrl();\n} catch (Exception $exception) {\n  if ($exception instanceof UrlNotFoundException) {\n    // Log it, or send it to an error management system...\n    // Display a 404 page...\n  }\n}\n```\n\n### 4. Catching method not allowed exceptions\n\nIn 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).\n\n```php\nuse function Folded\\matchRequestedUrl;\nuse Folded\\Exceptions\\MethodNotAllowedException;\n\ntry {\n  matchRequestedUrl();\n} catch (Exception $exception) {\n  if ($exception instanceof MethodNotAllowedException) {\n    // Log it, or send it to an error management system...\n    // Display a 405 page...\n  }\n}\n```\n\n### 5. Naming a route\n\nIn this example, we will name a route.\n\n```php\nuse function Folded\\addGetRoute;\n\naddGetRoute(\"/\", function() {\n  echo \"welcome home\";\n}, \"home.index\");\n```\n\n### 6. Get the URL of a named route\n\nIn this example, we will get the name of a named route.\n\n```php\nuse function Folded\\addGetRoute;\nuse function Folded\\getRouteUrl;\n\naddGetRoute(\"/user/{user}/post/{post}\", function($user, $post) {\n  echo \"User $user posted $post\";\n}, \"user.post.show\");\n\necho getRouteUrl(\"user.post.show\", [\"user\" =\u003e 42, \"post\" =\u003e 1]); // string(15) \"/user/42/post/1\"\n```\n\n### 7. Redirect to a named route\n\nIn this example, we will redirect to the URL of a named route.\n\n```php\nuse function Folded\\addGetRoute;\nuse function Folded\\redirectToRoute;\n\naddGetRoute(\"/about-us\", function() {\n  echo \"\u003ch1\u003eAbout us\u003c/h1\u003e\";\n}, \"about-us.index\");\n\nredirectToRoute(\"about-us.index\");\n```\n\nBy 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:\n\n```php\nuse function Folded\\addGetRoute;\nuse function Folded\\redirectToRoute;\n\naddGetRoute(\"/about-us\", function() {\n  echo \"\u003ch1\u003eAbout us\u003c/h1\u003e\";\n}, \"about-us.index\");\n\nredirectToRoute(\"about-us.index\", 200);\n```\n\n### 8. Redirect to an URL\n\nIn this example, we will redirect to a plain URL.\n\n```php\nuse function Folded\\redirectToUrl;\n\nredirectToUrl(\"/about-us\");\n```\n\nBy 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:\n\n```php\nuse function Folded\\redirectToUrl;\n\nredirectToUrl(\"/\", 200);\n```\n\n### 9. Check if the current URL is the given one\n\nIn this example, we will check if the current url is the one we have in parameter.\n\n```php\nuse function Folded\\currentUrlIs;\n\nif (currentUrlIs(\"/\")) {\n  echo \"we are at the home page\";\n} else {\n  echo \"we are not at the home page\";\n}\n```\n\n### 10. Check if a route matches the current URL\n\nIn this example, we will check if a given route name matches the URL.\n\n```php\nuse function Folded\\currentRouteIs;\nuse function Folded\\addGetRoute;\n\naddGetRoute(\"/about-us\", function() {\n  echo \"\u003ch1\u003eAbout us\u003c/h1\u003e\";\n}, \"about-us.index\");\n\nif (currentRouteIs(\"about-us.index\")) {\n  echo \"this is the about us page\";\n} else {\n  echo \"this is not the about us page\";\n}\n```\n\nNote 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.\n\n```php\nuse function Folded\\currentRouteIs;\nuse function Folded\\addGetRoute;\n\naddGetRoute(\"/user/{user}\", function() {\n  echo \"\u003ch1\u003eUser detail\u003c/h1\u003e\";\n}, \"user.show\");\n\nif (currentRouteIs(\"user.show\", [\"user\" =\u003e 17])) {\n  echo \"this is the route /user/17\";\n} else {\n  echo \"this is not the route /user/17\";\n}\n```\n\n## Version support\n\n|        | 7.3 | 7.4 | 8.0 |\n| ------ | --- | --- | --- |\n| v0.1.0 | ❌  | ✔️  | ❓  |\n| v0.1.1 | ❌  | ✔️  | ❓  |\n| v0.1.2 | ❌  | ✔️  | ❓  |\n| v0.1.3 | ❌  | ✔️  | ❓  |\n| v0.2.0 | ❌  | ✔️  | ❓  |\n| v0.3.0 | ❌  | ✔️  | ❓  |\n| v0.4.0 | ❌  | ✔️  | ❓  |\n| v0.4.1 | ❌  | ✔️  | ❓  |\n| v0.5.0 | ❌  | ✔️  | ❓  |\n| v0.5.1 | ❌  | ✔️  | ❓  |\n| v0.6.0 | ❌  | ✔️  | ❓  |\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffolded-php%2Frouting","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffolded-php%2Frouting","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffolded-php%2Frouting/lists"}