Download the PHP package sectoroverload2k/rest-router without Composer
On this page you can find all versions of the php package sectoroverload2k/rest-router. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download sectoroverload2k/rest-router
More information about sectoroverload2k/rest-router
Files in sectoroverload2k/rest-router
Informations about the package rest-router
REST Router for PHP
This code provides the basic framework for a PHP developer that wants to build a REST service.
Some of the features provided in this framework are:
- Multiple request methods (GET, POST, PUT, DELETE)
- Custom URL routing to support variables
- Automatic method name resolution based on HTTP verbs
Table of Contents
- Basic Routing
- Controller Structure
- Automatic Route Resolution
- Custom Routes
- Route Parameters
- Best Practices
Basic Routing
URL Structure
Example Request: GET /users/profile
- Default controller is index
- Default action is index
How the Router Works
The REST Router will:
- Parse the incoming request URL
- Load the appropriate controller file from the
/controllersdirectory - Instantiate the controller class
- Call the appropriate method based on HTTP verb + action
Controller Structure
File Naming Convention
Controllers should follow your application's naming convention. Common patterns:
/controllers/c_users.php(with prefix)
Class Example
Automatic Route Resolution
The router automatically resolves routes by combining the HTTP method + action name.
Simple Routes (Automatic)
These routes work without needing explicit rules in routes.php:
| HTTP Request | Controller Method |
|---|---|
GET /users/profile |
Users->get_profile() |
PUT /users/profile |
Users->put_profile() |
DELETE /users/profile |
Users->delete_profile() |
POST /users/create |
Users->post_create() |
Method Naming Pattern
Examples:
Users->get_profile()- handles GET requests to/users/profileAuth->post_login()- handles POST requests to/auth/loginResource->put_update()- handles PUT requests to/resource/updateResource->delete_item()- handles DELETE requests to/resource/item
Fallback Behavior
If a method-specific function is not found (e.g., get_profile()), the router will fall back to calling just the action name (e.g., profile()).
Custom Routes
When to Use Custom Routes
Use explicit routes in routes.php when:
- Nested paths that would be misinterpreted by automatic routing
- Path parameters like IDs in the middle of the URL
- Special routing logic that doesn't follow the standard pattern
Nested Path Example
Problem: PUT /users/profile/password
- Without custom route: Router tries to call
put_profile($data['password']) - With custom route: Router calls
put_profile_password($data)
Solution in routes.php:
Order Matters!
Always define more specific routes first:
Route Parameters
Path Parameters with :parameter_name
Use :parameter_name syntax for dynamic URL segments:
Accessing Parameters in Controller
Parameters are passed to the controller method:
Multiple Parameters
Best Practices
1. Consistent Method Naming
2. Handle Request Data
3. Use Explicit Routes for Nested Paths
4. Validate Input
Complete Example
routes.php
users.php Controller
Request Methods
The REST Router supports the following HTTP methods:
- GET - Retrieve resources
- POST - Create new resources
- PUT - Update existing resources
- DELETE - Delete resources