2017 © Pedro Peláez
 

package lightrouter

Lightweight Router

image

mattvb91/lightrouter

Lightweight Router

  • Wednesday, June 28, 2017
  • by mattvb91
  • Repository
  • 1 Watchers
  • 0 Stars
  • 2 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

LightRouter, (*1)

, (*2)

LightRouter

Lightweight PHP router class. This is a test project. If you need a reliable & fully tested solution please check out FastRoute or AltoRouter., (*3)

Basic Usage

$router = new mattvb91\LightRouter\Router();
$router->addRoute('/', MyController::class);
$router->run();

Defining Routes

To add new routes use the $router->addRoute() method. If no action is defined it will default to index which will be needed on your controller., (*4)

You will need to pass the route path & either a controller or a closure. If you do not specify an action it will default to 'index' which will be required on your controller., (*5)

$router->addRoute('/', MyController::class);
$router->addRoute('/contact', MyController::class, 'contact');

$route->addRoute('/hello', function() {
    echo 'Hello world';
});

Defining parameters

Passing parameters into your controller actions can be done using a :parameter attribute in your route definition:, (*6)

$router->addRoute('/view/:param', MyController::class);

Your method parameter must match the defined route parameter. In our example above our controllers view method would look like this:, (*7)

public function view($param)
{
}

Automatically Injecting Models

If you are using the LightModel ORM package for your DB models you can automatically inject the associated model by specifying the instance type in your controller action:, (*8)

//Your route definition
$router->addRoute('/user/view/:user', UserController::class, 'view');

//In your UserController::class
public function view(User $user)
{
    //$user is already fully loaded for you.
}

If you are using your own ORM or other DB model class you can also implement the LightRouteModelInterface::class in your custom model which expects you to return the fully loaded instance., (*9)

LightRouter will pass the associated $key from your route into the getForLightRoute method:, (*10)

public class CustomModel implements LightRouteModelInterface
{
    public static function getForLightRoute($routeParam): LightRouteModelInterface
    {
        //Use $routeParam to load your model class and return the instance
    }
}

The Versions

28/06 2017

dev-master

9999999-dev

Lightweight Router

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

by Matthias von Bargen

28/06 2017

0.1.0

0.1.0.0

Lightweight Router

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

by Matthias von Bargen

28/06 2017

dev-develop

dev-develop

Lightweight Router

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

by Matthias von Bargen