Search by

kodhe / http

karyakode

Kodhe HTTP - Request, Response, Middleware & Pipeline

Package info

github.com/kodhe/http

pkg:composer/kodhe/http

Statistics

Installs: 13

Dependents: 2

Suggesters: 0

Stars: 0

Open Issues: 0

1.1.0 2026-09-28 21:40 UTC

This package is not auto-updated.

Last update: 2026-10-28 10:33:05 UTC


README

PHP Version License Version

A comprehensive HTTP library for the Kodhe framework providing Request, Response, Middleware, Pipeline, and Routing systems.

๐Ÿ“ฆ Installation

composer require kodhe/http

Requirements

  • PHP >= 8.1
  • Composer

๐Ÿš€ Key Features

  • Request Handling - HTTP request handling (GET, POST, cookies, files, environment)
  • Response Management - Creating and managing HTTP responses with status codes and headers
  • Middleware System - Flexible middleware system with pipeline support
  • Pipeline Architecture - Request processing pipeline with exception handling
  • Advanced Routing - Modern router with support for:
    • Named routes
    • Route groups
    • API versioning
    • Subdomain routing
    • Resource routes
    • Rate limiting
  • Controller System - Base controller with theme binding
  • Helper Functions - URL and routing helpers

๐Ÿ“ Package Structure

http/
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ Controllers/
โ”‚   โ”‚   โ”œโ”€โ”€ BaseController.php
โ”‚   โ”‚   โ”œโ”€โ”€ Controller.php
โ”‚   โ”‚   โ””โ”€โ”€ RESTController.php
โ”‚   โ”œโ”€โ”€ Kernel/
โ”‚   โ”‚   โ”œโ”€โ”€ Kernel.php
โ”‚   โ”‚   โ””โ”€โ”€ Pipeline.php
โ”‚   โ”œโ”€โ”€ Middleware/
โ”‚   โ”‚   โ”œโ”€โ”€ Middleware.php
โ”‚   โ”‚   โ”œโ”€โ”€ MiddlewareGroup.php
โ”‚   โ”‚   โ”œโ”€โ”€ MiddlewareInterface.php
โ”‚   โ”‚   โ”œโ”€โ”€ MiddlewareRegistry.php
โ”‚   โ”‚   โ””โ”€โ”€ CallableMiddleware.php
โ”‚   โ”œโ”€โ”€ Requests/
โ”‚   โ”‚   โ”œโ”€โ”€ FormRequest.php
โ”‚   โ”‚   โ””โ”€โ”€ ValidatesRequests.php
โ”‚   โ”œโ”€โ”€ Routing/
โ”‚   โ”‚   โ”œโ”€โ”€ Router.php
โ”‚   โ”‚   โ”œโ”€โ”€ Route.php
โ”‚   โ”‚   โ”œโ”€โ”€ RouteCollection.php
โ”‚   โ”‚   โ”œโ”€โ”€ RouteItem.php
โ”‚   โ”‚   โ”œโ”€โ”€ RoutingManager.php
โ”‚   โ”‚   โ”œโ”€โ”€ UnifiedRouter.php
โ”‚   โ”‚   โ””โ”€โ”€ ...
โ”‚   โ”œโ”€โ”€ JsonResponse.php
โ”‚   โ”œโ”€โ”€ RedirectResponse.php
โ”‚   โ”œโ”€โ”€ Request.php
โ”‚   โ”œโ”€โ”€ Response.php
โ”‚   โ”œโ”€โ”€ ResponseFactory.php
โ”‚   โ”œโ”€โ”€ Uri.php
โ”‚   โ””โ”€โ”€ helpers/
โ”‚       โ”œโ”€โ”€ url.php
โ”‚       โ””โ”€โ”€ routing.php
โ”œโ”€โ”€ tests/
โ”‚   โ””โ”€โ”€ Unit/
โ”‚       โ”œโ”€โ”€ RequestTest.php
โ”‚       โ”œโ”€โ”€ ResponseTest.php
โ”‚       โ”œโ”€โ”€ UriTest.php
โ”‚       โ”œโ”€โ”€ Kernel/
โ”‚       โ”œโ”€โ”€ Middleware/
โ”‚       โ”œโ”€โ”€ Routing/
โ”‚       โ””โ”€โ”€ Helpers/
โ”œโ”€โ”€ composer.json
โ””โ”€โ”€ phpunit.xml

๐Ÿ’ก Usage

Request

use Kodhe\Framework\Http\Request;

// Create a request from globals
$request = Request::fromGlobals();

// Access data
$getId = $request->get('id');
$postData = $request->post('data');
$method = $request->getMethod();
$isAjax = $request->isAjax();

// JSON body
$jsonData = $request->json();

Response

use Kodhe\Framework\Http\Response;

$response = new Response();

// Set status and body
$response->setStatus(200);
$response->setBody('Hello World');

// Set headers
$response->setHeader('Content-Type', 'application/json');

// JSON response
$response->asJson(['message' => 'Success']);

// Redirect
$response->redirect('/dashboard');

Middleware

use Kodhe\Framework\Http\Middleware\Middleware;
use Kodhe\Framework\Http\Request;
use Kodhe\Framework\Http\Response;

class AuthMiddleware extends Middleware
{
    public function handle(Request $request, callable $next)
    {
        // Check authentication
        if (!$this->isAuthenticated()) {
            return $this->response->setStatus(401)->setBody('Unauthorized');
        }
        
        return $next($request);
    }
    
    protected function isAuthenticated()
    {
        // Your auth logic here
        return true;
    }
}

Pipeline

use Kodhe\Framework\Http\Kernel\Pipeline;
use Kodhe\Framework\Http\Request;
use Kodhe\Framework\Http\Response;

$pipeline = new Pipeline($request, $response);

// Add middlewares
$pipeline->pipe(AuthMiddleware::class)
         ->pipe(CorsMiddleware::class)
         ->pipeMany([
             RateLimitMiddleware::class,
             SanitizeMiddleware::class
         ]);

// Set handler
$pipeline->setHandler(function($request, $response, $params) {
    // Your controller logic
    return $response->setBody('Processed');
});

// Execute pipeline
$result = $pipeline->execute();

Routing

use Kodhe\Framework\Http\Routing\Router;

$router = new Router();

// Basic route
$router->get('/', 'HomeController@index');

// Named route
$router->get('/users', 'UserController@index')->name('users.index');

// Route with parameters
$router->get('/users/{id}', 'UserController@show');

// Route group with middleware
$router->group(['middleware' => ['auth']], function($router) {
    $router->get('/dashboard', 'DashboardController@index');
    $router->get('/profile', 'ProfileController@show');
});

// API routes with versioning
$router->apiGroup(['version' => 'v1'], function($router) {
    $router->get('/users', 'Api\UserController@index');
    $router->post('/users', 'Api\UserController@store');
});

// Resource route
$router->resource('posts', 'PostController');

// Generate URL for named route
$url = route('users.index');
$urlWithParams = route('users.show', ['id' => 1]);

Helper Functions

URL Helpers

// Base URL
$baseUrl = base_url();

// Full URL
$fullUrl = site_url('path/to/page');

// Current URL
$currentUrl = current_url();

// Previous URL
$prevUrl = previous_url();

Routing Helpers

// Generate named route URL
$url = route('users.index');

// API route with version
$apiUrl = api_route('api.users', ['version' => 'v2']);

// Subdomain route
$subdomainUrl = subdomain_route('blog.post', 'blog', ['slug' => 'my-post']);

๐Ÿงช Testing

Run the unit tests using PHPUnit:

cd http
composer install
vendor/bin/phpunit

Or with an XML configuration:

vendor/bin/phpunit --configuration phpunit.xml

๐Ÿ“‹ Class Overview

Core Classes

Class Description
Request HTTP request handler with GET, POST, JSON, file support
Response HTTP response builder with status codes and headers
Uri URI parser and builder
JsonResponse Specialized response for JSON
RedirectResponse Specialized response for redirects

Kernel

Class Description
Pipeline Request processing pipeline
Kernel Application kernel for bootstrap

Middleware

Class Description
Middleware Abstract base class for middleware
MiddlewareInterface Interface for middleware
MiddlewareGroup Grouping multiple middlewares
MiddlewareRegistry Registry for middleware management
CallableMiddleware Wrapper for closure-based middleware

Routing

Class Description
Router Main router with modern & legacy support
Route Individual route definition
RouteCollection Collection of routes
RouteItem Route item with metadata
RoutingManager Centralized routing management
UnifiedRouter Advanced router with a unified interface
RateLimiter Rate limiting for routes

Controllers

Class Description
BaseController Base controller with theme binding
Controller Standard controller implementation
RESTController RESTful API controller base

๐Ÿ”ง Configuration

The router can be configured with various options:

$router = new Router([
    'enable_modern_routing' => true,
    'enable_legacy_routing' => true,
    'prefer_modern' => true,
    'cache_routes' => true,
    'auto_detect_namespace' => true,
    'allow_namespace_in_routes' => true,
    'controller_suffix' => '',
    'default_404_controller' => 'FileNotFound',
    'default_404_namespace' => 'Kodhe\\Controllers\\Error\\'
]);

๐Ÿ“„ License

MIT License - see the LICENSE file for full details.

๐Ÿค Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

๐Ÿ“ž Support

For questions and issues, please open an issue in the repository or contact the development team.

Version: 1.1.0
Package: kodhe/http
Namespace: Kodhe\Framework\Http