Skip to content

1.0.7

Latest

Choose a tag to compare

@refkinscallv refkinscallv released this 06 Dec 07:12

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 βœ… 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! πŸŽ‰