Skip to content

Simple PHP router! Not quite production ready, but it is a work in progress,

License

Notifications You must be signed in to change notification settings

andrewthecodertx/php-simple-router

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SimpleRouter

SimpleRouter is a lightweight PHP routing library that helps you define and handle HTTP routes easily. It allows you to create routes for different HTTP methods (GET, POST) and execute corresponding callbacks when a matching route is found.

Getting Started

Installation

You can include the SimpleRouter class in your project by copying the provided Router class file into your project directory.

Usage

  1. Define Routes: Use get and post methods of the Router class to define routes. The routes consist of a path and a callback that will be executed when the route is matched.
// Using a closure
Router::get('/info', function() {
  phpinfo();
});

// Using a controller method
Router::get('/user/{name}', [Controllers\Controller::class, 'show']);
Router::post('/submit', [Controllers\Controller::class, 'submitForm']);

You can use dynamic segments in the path by enclosing them in curly braces.

  1. Dispatch Routes: Create a Request object representing the incoming HTTP request and pass it to the dispatch method of the Router to find and execute the matching route.
$request = new Request('GET', 'home/John');
$response = Router::dispatch($request);
  1. Handle Responses: The dispatch method returns a Response object generated by the matched route's callback. You can then handle and manipulate the response as needed.
echo $response->getBody();

Contributing

Contributions are welcome! Feel free to submit a pull request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

Simple PHP router! Not quite production ready, but it is a work in progress,

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages