Skip to content

ViniciusKendy17/php.init-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP_API_FRAMEWORK

A lightweight, fast PHP API routing framework. Build clean REST APIs without the bloat.


Overview

A minimal and elegant routing system for PHP APIs. Define your routes, handle requests, and respond with JSON—that's it.


Quick Start

Define a Route

// In Routes.php
route("GET", "/hello", function () {
    echo "Hello World";
});

route("POST", "/login", function () {
    Auth()->Login();
});

Send a Response

response()
    ->json(["message" => "Success"])
    ->setStatusCode(200)
    ->Send();

How It Works

1. Route Registration

Define routes using the route() function with:

  • HTTP method (GET, POST, DELETE, PUT, PATCH, etc)
  • Path (can include dynamic parameters)
  • Callback function (your handler code)

When a request arrives, the framework matches it to a route and executes the callback.

2. Dynamic Path Parameters

Extract values directly from the URL:

route("GET", "/cuts/:id/teste", function ($id) {
    echo "You requested item: " . $id;
});

Or use regex patterns:

route("DELETE", "/remove/user/(\d+)", function ($id) {
    // $id is guaranteed to be numeric
});

3. Query String Parameters

Access query parameters through PHP's superglobal:

route("GET", "/cuts", function () {
    $page = $_GET['page'] ?? 1;
    echo "Page: " . $page;
});

4. Request Dispatching

The dispatch() function:

  1. Converts your path patterns into regex
  2. Matches incoming requests against all routes
  3. Extracts path parameters automatically
  4. Executes the matching callback with those parameters

5. Response Handling

The Response class provides a fluent interface for sending JSON:

response()
    ->json([
        "status" => "error",
        "message" => "Not found"
    ])
    ->setStatusCode(404)
    ->Send();

Usage Examples

Simple endpoint:

route("GET", "/hello", function () {
    response()->json(["greeting" => "Hello World"])->setStatusCode(200)->Send();
});

With parameters:

route("GET", "/users/:id", function ($id) {
    $user = User::find($id);
    response()->json($user)->setStatusCode(200)->Send();
});

With controller:

route("POST", "/signup", function () {
    Auth()->Registrar();
});

Project Structure

File Purpose
Routes.php Define all your API routes
route_dispatch.php Core engine that matches URLs to routes
Response.php Helper class for JSON responses with status codes
index.php Entry point that starts the dispatcher
controllers/ Your business logic and handlers

Status

Currently in development. Use at your own risk.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages