PHPackages                             struktal/struktal-router - 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. [Framework](/categories/framework)
4. /
5. struktal/struktal-router

ActiveLibrary[Framework](/categories/framework)

struktal/struktal-router
========================

PHP router for the Struktal framework

2.0.0(10mo ago)01.7k↑271.4%[1 issues](https://github.com/Struktal/struktal-router/issues)1MITPHPPHP &gt;=8.2.0

Since Jun 13Pushed 6mo agoCompare

[ Source](https://github.com/Struktal/struktal-router)[ Packagist](https://packagist.org/packages/struktal/struktal-router)[ RSS](/packages/struktal-struktal-router/feed)WikiDiscussions main Synced 3d ago

READMEChangelog (5)DependenciesVersions (6)Used By (1)

struktal-router
===============

[](#struktal-router)

This is a PHP library that enables route handling in web applications.

Installation
============

[](#installation)

To install this library, include it in your project using Composer:

```
composer require struktal/struktal-router
```

Usage
=====

[](#usage)

Before you can use this library, you need to customize a few parameters. You can do this in the startup of your application:

```
\struktal\Router\Router::setPagesDirectory("path/to/your/pages");
\struktal\Router\Router::setAppUrl("https://yourdomain.com");
\struktal\Router\Router::setAppBaseUri("/"); // Or if you want to use a subdirectory, e.g. "/your-app/"
\struktal\Router\Router::setStaticDirectoryUri("static/");
```

Then, you can use the library's features in your code.

Defining routes
---------------

[](#defining-routes)

To define routes, you can use the `Router` class to register your routes.

A very simple example would be:

```
\struktal\Router\Router::addRoute(
    "GET",
    "/",
    "index.php",
    "index"
);
```

This registers a route that responds to `GET` requests at the root URL (`/`) and serves the `index.php` file, which is expected to handle the request. The `index.php` file must be located in the directory specified by `setPagesDirectory`.

You can also define routes with parameters:

```
\struktal\Router\Router::addRoute(
    "GET",
    "/user/{i:userId}",
    "users/details.php",
    "user_details"
);
```

This registers a route that responds to `GET` requests at `/user/{i:userId}`, where `{i:userId}` is a placeholder for an integer parameter named `userId`. The request will be handled by the `users/details.php` file, so the `details.php` file is now located in the subdirectory `users/` within the directory specified by `setPagesDirectory`.

A route can have multiple placeholders, all of them are denoted by curly braces with the data type and name of the placeholder inside. The variable is then available in the `$_GET` superglobal array, so you can access it like this:

```
$userId = $_GET['userId'];
```

The available data types for placeholders are:

- `b` for boolean values
- `f` for floating-point numbers
- `i` for integers
- `s` for strings

Finally, it is also possible to define routes that respond to multiple HTTP methods:

```
\struktal\Router\Router::addRoute(
    "GET|POST"
    "/user",
    "user.php",
    "user"
);
```

In this case, the route will respond to both `GET` and `POST` requests at the `/user` URL, and the handling `user.php` script could serve an HTML form to create or update a user upon a `GET` request, and process the form submission upon a `POST` request. However, this use-case is not recommended, as it is better to separate the handling of different HTTP methods into different routes for clarity and maintainability.

Defining error routes
---------------------

[](#defining-error-routes)

After defining your routes, you have to define error routes. When stumbling upon a routing error, the users have to be redirected accordingly. You can do this as follows:

```
\struktal\Router\Router::setError400Route(\strukral\Router\Router::generate("400"));
\struktal\Router\Router::setError404Route(\struktal\Router\Router::generate("404"));
```

This will redirect the users to the existing routes named `400` and `404` to handle the respective errors.

Starting the router
-------------------

[](#starting-the-router)

When you have followed the above steps, you can simply start the router in your application:

```
$router = new \struktal\Router\Router();
$router->startRouter();
```

More features
-------------

[](#more-features)

### Generate routes

[](#generate-routes)

You can generate URLs for your defined routes using the `generate` method of the `Router` class:

```
$url = \struktal\Router\Router::generate("user_details", ["userId" => 42]);
```

This searches for the route named `user_details` and replaces the `{i:userId}` placeholder with the value `42`, resulting in a URL like `/user/42`. You can pass an additional parameter `withHostUrl` as boolean to include the host URL in the generated output, resulting in a full URL like `https://yourdomain.com/user/42`.

### Get paths for static files

[](#get-paths-for-static-files)

You can also get the paths for static files using the `getStaticFilePath` method:

```
$staticFilePath = \struktal\Router\Router::staticFilePath("css/style.css");
```

License
=======

[](#license)

This software is licensed under the MIT license. See the [LICENSE](LICENSE) file for more information.

###  Health Score

39

—

LowBetter than 84% of packages

Maintenance61

Regular maintenance activity

Popularity20

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity53

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 ~21 days

Total

5

Last Release

302d ago

Major Versions

1.2.1 → 2.0.02025-09-06

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/49905418?v=4)[Jens Ostertag](/maintainers/JensOstertag)[@JensOstertag](https://github.com/JensOstertag)

---

Top Contributors

[![JensOstertag](https://avatars.githubusercontent.com/u/49905418?v=4)](https://github.com/JensOstertag "JensOstertag (13 commits)")

---

Tags

router

### Embed Badge

![Health badge](/badges/struktal-struktal-router/health.svg)

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

###  Alternatives

[slim/slim

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

12.3k52.9M1.4k](/packages/slim-slim)[klein/klein

A lightning fast router for PHP

2.7k1.1M31](/packages/klein-klein)[slim/slim-skeleton

A Slim Framework skeleton application for rapid development

1.7k464.5k6](/packages/slim-slim-skeleton)[pecee/simple-router

Simple, fast PHP router that is easy to get integrated and in almost any project. Heavily inspired by the Laravel router.

675231.0k18](/packages/pecee-simple-router)[rareloop/router

A powerful PHP Router for PSR7 messages inspired by the Laravel API

92186.3k5](/packages/rareloop-router)[yiisoft/router

Yii router

62360.1k26](/packages/yiisoft-router)

PHPackages © 2026

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