PHPackages                             kevinpirnie/kpt-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. kevinpirnie/kpt-router

ActiveLibrary[Framework](/categories/framework)

kevinpirnie/kpt-router
======================

A comprehensive PHP routing library with middleware support, rate limiting, view rendering, and controller resolution capabilities

v1.0.33(3mo ago)019[1 PRs](https://github.com/kpirnie/kp-router/pulls)MITPHPPHP &gt;=8.4CI passing

Since Jan 25Pushed 3mo agoCompare

[ Source](https://github.com/kpirnie/kp-router)[ Packagist](https://packagist.org/packages/kevinpirnie/kpt-router)[ Docs](https://github.com/kpirnie/kpt-router)[ Fund](https://ko-fi.com/kevinpirnie)[ Fund](https://www.paypal.biz/kevinpirnie)[ RSS](/packages/kevinpirnie-kpt-router/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (3)Versions (4)Used By (0)

KPT Router
==========

[](#kpt-router)

A comprehensive PHP routing library with middleware support, rate limiting, view rendering, and controller resolution capabilities.

Features
--------

[](#features)

- **HTTP Method Support**: Full support for GET, POST, PUT, PATCH, DELETE, HEAD, TRACE, and CONNECT methods
- **Middleware Pipeline**: Global and route-specific middleware with execution control
- **Rate Limiting**: Built-in rate limiting with Redis and file-based storage backends
- **View Rendering**: Template rendering system with data sharing and caching
- **Controller Resolution**: Automatic controller instantiation and method calling
- **Route Parameters**: Dynamic route parameters with named capture groups
- **Error Handling**: Comprehensive error handling and logging
- **Caching**: Built-in caching support for views and controllers
- **Method Override**: Support for HTTP method override in forms

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

[](#requirements)

- PHP 8.0 or higher
- Redis extension (optional, for Redis-based rate limiting)

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

[](#installation)

Install via Composer:

```
composer require kpirnie/kpt-router
```

Web Server Configuration
------------------------

[](#web-server-configuration)

For the router to work properly, you need to configure your web server to redirect all requests to your main PHP file (usually `index.php`). Here are the configurations for popular web servers:

### Apache (.htaccess)

[](#apache-htaccess)

Create an `.htaccess` file in your document root:

```
RewriteEngine On

# Handle Angular and other client-side routes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]

# Optional: Redirect trailing slashes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]

# Security headers (optional)

    Header always set X-Content-Type-Options nosniff
    Header always set X-Frame-Options DENY
    Header always set X-XSS-Protection "1; mode=block"

```

### Nginx

[](#nginx)

Add this to your Nginx server block:

```
server {
    listen 80;
    server_name your-domain.com;
    root /path/to/your/app;
    index index.php;

    # Route all requests to index.php
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    # PHP handler
    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock; # Adjust PHP version
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    # Security: Deny access to sensitive files
    location ~ /\. {
        deny all;
    }

    location ~ /(vendor|tmp|cache)/ {
        deny all;
    }
}
```

### IIS (web.config)

[](#iis-webconfig)

For Windows IIS servers, create a `web.config` file:

```

```

### Built-in PHP Server (Development)

[](#built-in-php-server-development)

For development, you can use PHP's built-in server:

```
# From your app directory
php -S localhost:8000 -t . index.php
```

Or create a simple router file (`router.php`):

```
