2017 © Pedro Peláez
 

library router

Router for PHP. Simple, lightweight and convenient.

image

delight-im/router

Router for PHP. Simple, lightweight and convenient.

  • Wednesday, January 24, 2018
  • by delight-im
  • Repository
  • 3 Watchers
  • 17 Stars
  • 325 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 3 Forks
  • 0 Open issues
  • 5 Versions
  • 18 % Grown

The README.md

PHP-Router

Router for PHP. Simple, lightweight and convenient., (*1)

Requirements

  • PHP 5.6.0+

Installation

  1. Include the library via Composer [?]:, (*2)

    $ composer require delight-im/router
    
  2. Include the Composer autoloader:, (*3)

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

Usage

  1. Enable URL rewriting on your web server, (*4)

    • Apache (in .htaccess or httpd.conf), (*5)

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

      try_files $uri /index.php;
      
  2. Create a new Router instance, (*7)

    • for the web root, (*8)

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

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

    • Static route:, (*11)

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

      $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., (*13)

    • Route with multiple supported request methods:, (*14)

      $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, (*15)

    // 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), (*16)

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

    and, (*17)

    $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., (*18)

License

This project is licensed under the terms of the MIT License., (*19)

The Versions

24/01 2018

dev-master

9999999-dev https://github.com/delight-im/PHP-Router

Router for PHP. Simple, lightweight and convenient.

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

http route routing router

23/12 2016

v3.1.0

3.1.0.0 https://github.com/delight-im/PHP-Router

Router for PHP. Simple, lightweight and convenient.

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

http route routing router

05/09 2016

v3.0.0

3.0.0.0 https://github.com/delight-im/PHP-Router

Router for PHP. Simple, lightweight and convenient.

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

http route routing router

22/07 2016

v2.0.0

2.0.0.0 https://github.com/delight-im/PHP-Router

Router for PHP. Simple, lightweight and convenient.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

http route routing router

06/06 2016

v1.0.0

1.0.0.0 https://github.com/delight-im/PHP-Router

Router for PHP. Simple, lightweight and convenient.

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.0

 

http route routing router