Giter Site home page Giter Site logo

bensonosei / route-me Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 0.0 28 KB

Route-Me is a lightweight and flexible PHP router package for creating RESTful APIs. It supports route parameters, middleware, and PSR-7 request/response handling. Route-Me is built using the PSR-7 HTTP message interfaces and can be used with any PSR-7 implementation.

License: MIT License

PHP 100.00%

route-me's Introduction

benson/router-me

Description

Route-Me is a lightweight and flexible PHP router package for creating RESTful APIs. It supports route parameters, middleware, and PSR-7 request/response handling. Route-Me is built using the PSR-7 HTTP message interfaces and can be used with any PSR-7 implementation. It is also framework-agnostic and can be used with any PHP framework or standalone application. Route-Me is developed and maintained by Benson Osei-Mensah.

Installation

To install Route-Me, run the following command in your project directory:

composer require benson/router-me

Table of Contents

Features

  • Route Definition: Add routes to the router using the addRoute() method.
  • Middleware: Apply middleware functions to specific routes or globally using the withMiddleware() method.
  • Request Handling: Handle incoming requests by matching URL and HTTP method against defined routes.
  • Error Handling: Handle 404 Not Found scenarios and custom error handling logic.
  • Route Parameters: Support for route parameters defined within the URL pattern.
  • Flexible Callbacks: Use closures or class methods as callback functions.
  • PSR-7 Support: Built-in support for JSON request/response handling.

Usage

To use Route-Me in your project, follow these steps:

  1. Import the composer autoloader into your PHP file:

    require_once 'vendor/autoload.php';
  2. Create a new instance of the Router class:

    $router = new Router();
  3. Add routes to the router using the addRoute() method:

    $router->get('/users', function () {
        // Handle GET request to /users
    });
    
    // Add class method as callback
    $router->addRoute('GET', '/users', "App\Controllers\UserController@index");
    
    // Add route parameters
    $router->addRoute('GET', '/users/{id}', function ($id) {
        // Handle GET request to /users/{id}
    });
    
    // Add middleware
    $router->addRoute(
        'GET', // HTTP method
        '/user/{id}', // URL pattern
        'App\Controllers\UserController@show', // Callback function
        'App\Middleware\Authenticate@handle' // Middleware function
        );
    
        // Add middleware to all routes
        $router->withMiddleware('App\Middleware\Authenticate@handle');
    
    $router->post('/users', function () {
        // Handle POST request to /users
    });
  4. Handle incoming requests by calling the run() method:

    $router->run(); 

Contribution

Contributions are welcome! To contribute to Route-Me, follow these steps:

  1. Fork the repository.

  2. Create a new branch.

    git checkout -b your-branch-name
  3. Make your changes.

  4. Commit your changes and push them to your branch. Make sure to add a clear commit message and include any relevant documentation updates. Since Route-Me follows the PSR-2 coding standards, please ensure that your code adheres to these standards. If you are not sure, you can use the PHP Coding Standards Fixer

    git add .
    git commit -m "your commit message"
    git push origin your-branch-name
  5. Submit a pull request. Please ensure that you adhere to the coding standards and write clear commit messages.

License

Route-Me is open-source software released under the MIT License. See LICENSE for details.

Author

Route-Me is developed and maintained by Benson Ose-Mensah.

route-me's People

Contributors

bensonosei avatar

Stargazers

 avatar Umair Durrani avatar Mohammed Amin Ibrahim avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.