Skip to content

Releases: refkinscallv/php-route

1.0.7

06 Dec 07:12

Choose a tag to compare

PHP Route v1.0.7 Release

Release Date: December 6, 2024

🎉 What's New

Major Features

1️⃣ Multiple Middleware Format Support

Three ways to define middleware - choose what works best for you:

// Closure
Route::get('/path', $handler, [
    function($req, $res) { return true; }
]);

// Static class
Route::get('/path', $handler, ['AuthMiddleware@check']);

// Instance
Route::get('/path', $handler, [new RateLimitMiddleware(100), 'check']);

2️⃣ Middleware Attribute Persistence

Middleware can now set attributes that are accessible in route handlers:

// Middleware sets attribute
$req->withAttribute('user_id', 123);

// Handler accesses it
$userId = $req->getAttribute('user_id'); // 123

3️⃣ Fixed Optional Route Parameters

Optional parameters {?paramName} now work correctly:

Route::get('/post/{?slug}', function($req, $res) {
    $slug = $req->getAttribute('slug') ?? 'default';
    $res->json(['slug' => $slug]);
});

// All these work now:
// GET /post → slug = null
// GET /post/hello → slug = "hello"
// GET /post/2 → slug = "2" ✅ FIXED!

4️⃣ Middleware Control Flow

Middleware now supports proper return value control:

function($req, $res) {
    if (!$req->getHeaderLine('Authorization')) {
        $res->json(['error' => 'Unauthorized'], 401);
        return false;  // Stop request
    }
    return true;  // Continue
}

🔧 Technical Improvements

Request Class

  • Direct attribute modification (no more cloning)
  • Custom attribute storage for faster access
  • Merged attribute access from all sources

Route Class

  • Improved regex generation for optional parameters
  • Better middleware execution pipeline
  • Proper attribute persistence across chain
  • Query string removal in URI normalization

📊 Regex Pattern Examples

Optional parameters now generate correct regex:

{?slug}              → #^/post(?:/(?P<slug>[^/]+))?/?$#
{?page:\d+}          → #^/page(?:/(?P<page>\d+))?/?$#
{id}/posts/{?tag}    → #^/user/(?P<id>[^/]+)/posts(?:/(?P<tag>[^/]+))?/?$#

✨ Example: Complete Feature Showcase

<?php
class AuthMiddleware {
    public static function check(Request $req, Response $res) {
        $token = $req->getHeaderLine('Authorization');
        if (empty($token)) {
            $res->json(['error' => 'Unauthorized'], 401);
            return false;
        }
    // Set user attributes
    $req-&gt;withAttribute('user_id', 123);
    $req-&gt;withAttribute('user_role', 'admin');
    return true;
}

}

// Route with optional parameter and middleware
Route::get('/user/{?filter}', function($req, $res) {
$userId = $req->getAttribute('user_id'); // From middleware
$filter = $req->getAttribute('filter'); // From URL

$res-&gt;json([
    'user_id' =&gt; $userId,
    'filter' =&gt; $filter ?? 'none'
]);

}, ['AuthMiddleware@check']);

// URLs that now work:
// GET /user → user_id=123, filter=null ✅
// GET /user/active → user_id=123, filter="active" ✅


✅ What's Fixed

Issue Before After
Optional params ❌ 404 error ✅ Works correctly
Middleware attributes ❌ Lost between middleware ✅ Persist to handler
Middleware formats ❌ Closures only ✅ 3 format types
Middleware control ⚠️ Limited ✅ Full control (true/false)

📈 Backward Compatibility

100% Backward Compatible

  • No breaking changes
  • All v1.0.6 code works as-is
  • Just add new features where needed

📚 Documentation

New guides added:

  • Optional parameters complete guide
  • Request attributes explanation
  • Middleware attribute usage examples
  • Route regex pattern testing utility
  • Troubleshooting section

🐛 Bug Fixes

  1. Optional parameters returning 404 - FIXED
  2. Middleware attributes not persisting - FIXED
  3. Query string interference - FIXED
  4. Multiple trailing slash handling - FIXED

🚀 Installation & Upgrade

Fresh Install

composer require refkinscallv/php-route

Upgrade from v1.0.6

composer update refkinscallv/php-route

No code changes required - just works!


📖 Learn More


🤝 Feedback

Found issues? Have suggestions?


📋 Release Checklist

  • ✅ Features implemented and tested
  • ✅ Documentation updated
  • ✅ Examples provided
  • ✅ Backward compatibility verified
  • ✅ No breaking changes

Thank you for using PHP Route! Happy routing! 🎉

For detailed changelog, see CHANGELOG.md

# PHP Route v1.0.7 Release

Release Date: December 6, 2024

🎉 What's New

Major Features

1️⃣ Multiple Middleware Format Support

Three ways to define middleware - choose what works best for you:

// Closure
Route::get('/path', $handler, [
    function($req, $res) { return true; }
]);

// Static class
Route::get('/path', $handler, ['AuthMiddleware@check']);

// Instance
Route::get('/path', $handler, [new RateLimitMiddleware(100), 'check']);

2️⃣ Middleware Attribute Persistence

Middleware can now set attributes that are accessible in route handlers:

// Middleware sets attribute
$req->withAttribute('user_id', 123);

// Handler accesses it
$userId = $req->getAttribute('user_id'); // 123

3️⃣ Fixed Optional Route Parameters

Optional parameters {?paramName} now work correctly:

Route::get('/post/{?slug}', function($req, $res) {
    $slug = $req->getAttribute('slug') ?? 'default';
    $res->json(['slug' => $slug]);
});

// All these work now:
// GET /post          → slug = null
// GET /post/hello    → slug = "hello"
// GET /post/2        → slug = "2"  ✅ FIXED!

4️⃣ Middleware Control Flow

Middleware now supports proper return value control:

function($req, $res) {
    if (!$req->getHeaderLine('Authorization')) {
        $res->json(['error' => 'Unauthorized'], 401);
        return false;  // Stop request
    }
    return true;  // Continue
}

🔧 Technical Improvements

Request Class

  • Direct attribute modification (no more cloning)
  • Custom attribute storage for faster access
  • Merged attribute access from all sources

Route Class

  • Improved regex generation for optional parameters
  • Better middleware execution pipeline
  • Proper attribute persistence across chain
  • Query string removal in URI normalization

📊 Regex Pattern Examples

Optional parameters now generate correct regex:

{?slug}              → #^/post(?:/(?P<slug>[^/]+))?/?$#
{?page:\d+}          → #^/page(?:/(?P<page>\d+))?/?$#
{id}/posts/{?tag}    → #^/user/(?P<id>[^/]+)/posts(?:/(?P<tag>[^/]+))?/?$#

✨ Example: Complete Feature Showcase

<?php
class AuthMiddleware {
    public static function check(Request $req, Response $res) {
        $token = $req->getHeaderLine('Authorization');
        if (empty($token)) {
            $res->json(['error' => 'Unauthorized'], 401);
            return false;
        }
        
        // Set user attributes
        $req->withAttribute('user_id', 123);
        $req->withAttribute('user_role', 'admin');
        return true;
    }
}

// Route with optional parameter and middleware
Route::get('/user/{?filter}', function($req, $res) {
    $userId = $req->getAttribute('user_id');    // From middleware
    $filter = $req->getAttribute('filter');     // From URL
    
    $res->json([
        'user_id' => $userId,
        'filter' => $filter ?? 'none'
    ]);
}, ['AuthMiddleware@check']);

// URLs that now work:
// GET /user              → user_id=123, filter=null ✅
// GET /user/active       → user_id=123, filter="active" ✅

✅ What's Fixed

Issue Before After
Optional params ❌ 404 error ✅ Works correctly
Middleware attributes ❌ Lost between middleware ✅ Persist to handler
Middleware formats ❌ Closures only ✅ 3 format types
Middleware control ⚠️ Limited ✅ Fu...
Read more

1.0.6

25 Oct 06:21

Choose a tag to compare

FIX : Http\Request can't fetch body from request

1.0.5

02 Oct 09:29

Choose a tag to compare

UPDATE : packagist and version

1.0.0

23 Sep 07:53

Choose a tag to compare

PHP Route v1.0.0 – Initial Release

Release Date: 2025-09-23

We are excited to announce the first stable release of PHP Route, a lightweight and flexible routing library for PHP applications. Version 1.0.0 provides a solid foundation for defining routes, handling requests, and managing middlewares with ease.

Features in v1.0.0

  • HTTP Route Support: Easily define routes for GET, POST, PUT, PATCH, DELETE, and OPTIONS methods.
  • Any Method Support: Use any() to handle all HTTP methods on a single route.
  • Route Grouping: Group routes under a common prefix with optional middlewares.
  • Global and Route-Specific Middlewares: Apply middlewares globally or per route for authentication, logging, or other logic.
  • Custom Error Handling: Define a global error handler to catch exceptions and respond with custom messages.
  • Maintenance Mode: Temporarily enable maintenance mode with a custom response.
  • Custom 404 Not Found: Define your own response when a route is not found.
  • PSR-7 Compatible Request & Response: Fully compatible with PSR-7 using Guzzle HTTP messages, allowing standardized handling of HTTP requests and responses.
  • Response Helpers: Send JSON, plain text, or redirects easily with the Response object.

Installation

Install via Composer:

composer require refkinscallv/php-route

Include the Composer autoloader:

require __DIR__ . '/vendor/autoload.php';

Usage Example

use RFRoute\Route\Route;
use RFRoute\Http\Request;
use RFRoute\Http\Response;

Route::get('/hello', function(Request $req, Response $res) {
    $res->send("Hello World!");
});

Route::setErrorHandler(function(Throwable $e, Request $req, Response $res) {
    $res->send("Something went wrong: " . $e->getMessage(), 500);
});

Route::dispatch($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']);

Contribution & Issues

Contributions are welcome! Please submit bug reports or feature requests via [GitHub Issues](https://github.com/refkinscallv/php-route/issues).