Giter Site home page Giter Site logo

router's Introduction

Pinga Router

Router for PHP. Simple, lightweight and convenient.

Requirements

  • PHP 8.1.0+

Installation

  1. Include the library via Composer [?]:

    $ composer require pinga/router
    
  2. Include the Composer autoloader:

    require __DIR__ . '/vendor/autoload.php';

Usage

  1. Enable URL rewriting on your web server

    • Apache (in .htaccess or httpd.conf)

      RewriteEngine On
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteRule . index.php [L]
      
    • Nginx (in nginx.conf)

      try_files $uri /index.php;
      
  2. Create a new Router instance

    • for the web root

      $router = new \Delight\Router\Router();
    • for any subdirectory

      $router = new \Delight\Router\Router('/my/base/path');
  3. Add some routes and map them to anonymous functions or closures

    • Static route:

      $router->get('/', function () {
          // do something
      });
    • Dynamic route (with parameters):

      $router->get('/users/:id/photo', function ($id) {
          // get the photo for user `$id`
      });

      The values of parameters matched in the URL can be captured as arguments in the callback.

    • Route with multiple supported request methods:

      $router->any([ 'POST', 'PUT' ], '/users/:id/address', function ($id) {
          // update the address for user `$id`
      });
  4. Map routes to controller methods instead for more complex callbacks

    // use static methods
    $router->get('/photos/:id/convert/:mode', [ 'PhotoController', 'myStaticMethod' ]);
    
    // or
    
    // instance methods
    $router->get('/photos/:id/convert/:mode', [ $myPhotoController, 'myInstanceMethod' ]);
  5. Inject arguments for access to further values and objects (prepended to those matched in the route)

    class MyController {
    
        public static function someStaticMethod($database, $uuid) {
            // do something
        }
    
    }

    and

    $database = new MyDatabase();
    
    // ...
    
    $router->delete('/messages/:uuid', [ 'MyController', 'someStaticMethod' ], [ $database ]);

Contributing

All contributions are welcome! If you wish to contribute, please create an issue first so that your feature, problem or question can be discussed.

License

This project is licensed under the terms of the MIT License.

router's People

Contributors

ocram avatar getpinga avatar

Forkers

tasoraso

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.