2017 © Pedro Peláez
 

library saffron

Tiny PHP router

image

krzysztof-magosa/saffron

Tiny PHP router

  • Saturday, December 10, 2016
  • by krzysztof-magosa
  • Repository
  • 5 Watchers
  • 38 Stars
  • 739 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 44 Versions
  • 0 % Grown

The README.md

Saffron PHP Router

Build Status Scrutinizer Code Quality Coverage Status, (*1)

What is Saffron?

Saffron is very fast and flexible PHP router for your application., (*2)

Version

I no longer develop software in PHP so I do not actively support this project. Recently due to contribution project has been updated to be sure it works with PHP 7.x. The newest tag supporting older PHP versions (and also HHVM) is 5.3. If you still use this project - contributions are welcome., (*3)

Features

  • No external dependencies
  • High performance
  • Method condition support
  • Domain condition support
  • Https/non-https condition support
  • Routes with optional parameters
  • Requirements for parameters
  • Reverse routing
  • Well tested, 100% of test coverage

Installation

You can easily install Saffron by adding below requirement to your composer.json, (*4)

{
    "require": {
        "krzysztof-magosa/saffron": "5.*"
    }
}

How to use

You need to use RouterFactory to create instance of Router. Constructor of RouterFactory accepts one parameter, Closure which configures routes. Closure gets RoutesCollection in the first parameter. Closure is fired only once, then everything is stored in compiled file., (*5)

use KM\Saffron\RouterFactory;

$factory = new RouterFactory(
    function ($collection) {
        // configuration of routes goes here...
        $collection->route('home')
            ->setUri('/')
            ->setTarget('HomeController');
    }
);

By default Saffron stores cache in system temporary directory. To avoid collisions between projects you are encouraged to set separate cache directories in each project hosted on the same server. If you really need to use one directory for more projects you can set class suffix., (*6)

$factory
    ->setCacheDir(__DIR__ . '/cache')
    ->setClassSuffix('MyProject')
    ->build();

When you have configured RouterFactory, you can build Router instance by calling build() method., (*7)

$router = $factory->build();

Configuring routes

use KM\Saffron\RouterFactory;

$factory = new RouterFactory(
    function ($collection) {
        $collection->route('home')
            ->setUri('/')
            ->setTarget('HomeController');

        $collection->route('contact')
            ->setUri('/contact')
            //...
            ->setTarget('ContactController');

        //...
    }
);

To add Route you need to call method route() on $collection, giving route name as the first parameter. Method returns Route instance and then you can set parameters on it. You can create as many routes as you want, but each one needs to have unique name., (*8)

Setting target

To execute controller after matching particular route you need to use setTarget() method. First parameter is class name of controller, second is method name. If you omit second parameter it will default to 'indexAction'., (*9)

$collection->route('home')
    ->setTarget('HomeController');

$collection->route('team')
    ->setTarget('TeamController', 'actionName');

Setting uri

To match request against uri you need to call method setUri() on Route instance. It takes only one parameter, expected uri., (*10)

$collection->route('contact')
    ->setUri('/contact');

Setting domain

To match request against domain you need to call method setDomain() on Route instance. It takes only one parameter, expected domain., (*11)

$collection->route('contact')
    ->setDomain('www.example.com');

Setting method

To match request against method you need to call method setMethod() on Route instance. You can pass one method as a string, or more using array., (*12)

$collection->route('api1')
    ->setMethod('GET');

$collection->route('api2')
    ->setMethod(['GET', 'POST']);

Setting https

You may want to allow access to some resources only via encrypted or unencrypted connection. It can be done using setHttps() method. Pass true to this method if you want only encrypted traffic, false if unecrypted. Null means that it doesn't matter (it's the default setting)., (*13)

$collection->route('secret')
    ->setHttps(true);

$collection->route('public')
    ->setHttps(false);

Using placeholders

If your uri or domain contains variable parts, you can catch them using placeholders. Placeholders are defined using curly braces., (*14)

$collection->route('contact')
    ->setUri('/contact/{name}')
    ->setDomain('{lang}.example.com');

This example allows you to use links like: * http://english.example.com/contact/webmaster * http://spanish.example.com/contact/author, (*15)

Sometimes you want to allow user to omit some placeholders in the link. You can use setDefaults() method to achieve this., (*16)

$collection->route('contact')
    ->setUri('/contact/{name}')
    ->setDomain('{lang}.example.com')
    ->setDefaults(
        [
            'name' => 'webmaster',
            'lang' => 'english',
        ]
    );

Now user can go into link http://example.com/contact, and lang will be 'english', and name will be 'webmaster'., (*17)

You can also set requirements for placeholders, to allow user only to use some values there. Requirements are build using regular expressions, the same which you use in the preg_match()., (*18)

$collection->route('contact')
    ->setUri('/contact/{name}')
    ->setDomain('{lang}.example.com')
    ->setDefaults(
        [
            'name' => 'webmaster',
            'lang' => 'english',
        ]
    )
    ->setRequirements(
        [
            'name' => '\w+',
            'lang' => 'english|spanish|french',
        ]
    );

Matching requests

Saffron accepts Request object. In typical configurations you can use createFromGlobals() static method. It was tested on Apache server with mod_php., (*19)

use KM\Saffron\Request;
$request = Request::createFromGlobals();

If your configuration isn't typical, you can create this object manually., (*20)

use KM\Saffron\Request;
$request = new Request();
$request
    ->setUri($uri)
    ->setDomain($domain)
    ->setMethod($method)
    ->setHttps($https);

Now you can pass this object to Router match() method which returns RoutingResult object., (*21)

$result = $router->match($request);

You can check whether matching was successful using isSuccessful() method. To check why matching was not successful you need to use two methods: isResourceNotFound() and isMethodNotAllowed(). When isResourceNotFound() returns true you should display error 404 to user, when isMethodNotAllowed() returns true you should display error 405. RFC 2616 requires to set Allow header containing allowed methods in case of displaying 405 error. You can get this list by calling getAllowedMethods(). Remembet that Saffron is not framework, but just router, so it's up to you to do that., (*22)

Saffron is two-directional router, so in addition to matching requests you can also build links. Router has method assemble() for building links., (*23)

// Building uri
$uri = $router->assemble('routeName', ['parameter1' => 'value1']);

// Building entire link (scheme + domain + uri)
$link = $router->assemble('routeName', ['parameter1' => 'value1'], true);

Executing controllers

After successful matching of request you can fire controller specified in the route using Executor., (*24)

use KM\Saffron\Executor;
$executor = new Executor($result);
$executor->fire();

In some cases there is need to do something with controller just before or/and just after executing action. It can be done by using setPreDispatch() and setPostDispatch()., (*25)

use KM\Saffron\Executor;
$executor = new Executor($result);
$executor
    ->setPreDispatch(
        function ($controller, $method, $parameters) {
            // do something before calling action
        }
    )
    ->setPostDispatch(
        function ($controller, $method, $parameters) {
            // do something after calling action
        }
    );

$executor->fire();

In some cases (for example firing error controller) you may want to fire controller which doesn't come from matched route. Executor also supports such situation., (*26)

$executor = new Executor();
$executor
    ->setController('YourController')
    ->setMethod('methodName')
    ->setParameters(['param1' => 'value1'])
    ->fire();

Getting parameters

If your uri or domain containts placeholders they will be passed to your controller in arguments to method. You just need to ensure that controller method has arguments with the same names as your placeholders. You don't have to catch all placeholders., (*27)

class Controller
{
    public function method($lang, $name)
    {
    }
}

The Versions

10/12 2016

dev-master

9999999-dev

Tiny PHP router

  Sources   Download

Apache-2.0

The Development Requires

by Krzysztof Magosa

routing router tiny

14/06 2015

dev-development

dev-development

Tiny PHP router

  Sources   Download

Apache-2.0

The Development Requires

by Krzysztof Magosa

routing router tiny

14/06 2015

5.3.0

5.3.0.0

Tiny PHP router

  Sources   Download

Apache-2.0

The Development Requires

by Krzysztof Magosa

routing router tiny

26/11 2014

5.2.3

5.2.3.0

Tiny PHP router

  Sources   Download

Apache-2.0

The Development Requires

by Krzysztof Magosa

routing router tiny

26/11 2014

5.2.2

5.2.2.0

Tiny PHP router

  Sources   Download

Apache-2.0

The Development Requires

by Krzysztof Magosa

routing router tiny

18/11 2014

5.2.1

5.2.1.0

Tiny PHP router

  Sources   Download

Apache-2.0

The Development Requires

by Krzysztof Magosa

routing router tiny

10/11 2014

5.2.0

5.2.0.0

Tiny PHP router

  Sources   Download

Apache-2.0

The Development Requires

by Krzysztof Magosa

routing router tiny

07/11 2014

5.1.1

5.1.1.0

Tiny PHP router

  Sources   Download

Apache-2.0

The Development Requires

by Krzysztof Magosa

routing router tiny

28/10 2014

5.1.0

5.1.0.0

Tiny PHP router

  Sources   Download

Apache-2.0

The Development Requires

by Krzysztof Magosa

routing router tiny

28/10 2014

5.0.1

5.0.1.0

Tiny PHP router

  Sources   Download

Apache-2.0

The Development Requires

by Krzysztof Magosa

routing router tiny

25/10 2014

dev-readme

dev-readme

Tiny PHP router

  Sources   Download

Apache-2.0

The Development Requires

by Krzysztof Magosa

routing router tiny

21/10 2014

5.0.0

5.0.0.0

Tiny PHP router

  Sources   Download

Apache-2.0

The Development Requires

by Krzysztof Magosa

routing router tiny

20/10 2014

5.0.0-beta2

5.0.0.0-beta2

Tiny PHP router

  Sources   Download

Apache-2.0

The Development Requires

by Krzysztof Magosa

routing router tiny

20/10 2014

5.0.0-beta

5.0.0.0-beta

Tiny PHP router

  Sources   Download

Apache-2.0

The Development Requires

by Krzysztof Magosa

routing router tiny

19/10 2014

dev-devel

dev-devel

Tiny PHP router

  Sources   Download

Apache-2.0

The Development Requires

by Krzysztof Magosa

routing router tiny

13/10 2014

4.2.1

4.2.1.0

Tiny PHP router

  Sources   Download

Apache-2.0

The Development Requires

by Krzysztof Magosa

routing router tiny

13/10 2014

4.2.0

4.2.0.0

Tiny PHP router

  Sources   Download

Apache-2.0

The Development Requires

by Krzysztof Magosa

routing router tiny

12/10 2014

4.1.0

4.1.0.0

Tiny PHP router

  Sources   Download

Apache-2.0

The Development Requires

by Krzysztof Magosa

routing router tiny

12/10 2014

4.0

4.0.0.0

Tiny PHP router

  Sources   Download

Apache-2.0

The Development Requires

by Krzysztof Magosa

routing router tiny

12/10 2014

3.2.2

3.2.2.0

Tiny PHP router

  Sources   Download

Apache-2.0

The Development Requires

by Krzysztof Magosa

routing router tiny

12/10 2014

3.2.1

3.2.1.0

Tiny PHP router

  Sources   Download

Apache-2.0

The Development Requires

by Krzysztof Magosa

routing router tiny

12/10 2014

3.2.0

3.2.0.0

Tiny PHP router

  Sources   Download

Apache-2.0

The Development Requires

by Krzysztof Magosa

routing router tiny

12/10 2014

3.1.0

3.1.0.0

Tiny PHP router

  Sources   Download

Apache-2.0

The Development Requires

by Krzysztof Magosa

routing router tiny

11/10 2014

3.0.0

3.0.0.0

Tiny PHP router

  Sources   Download

Apache-2.0

The Development Requires

by Krzysztof Magosa

routing router tiny

11/10 2014

dev-km-state

dev-km-state

Tiny PHP router

  Sources   Download

Apache-2.0

The Development Requires

by Krzysztof Magosa

routing router tiny

11/10 2014

2.4.2

2.4.2.0

Tiny PHP router

  Sources   Download

Apache-2.0

The Development Requires

by Krzysztof Magosa

routing router tiny

11/10 2014

2.4.1

2.4.1.0

Tiny PHP router

  Sources   Download

Apache-2.0

The Development Requires

by Krzysztof Magosa

routing router tiny

10/10 2014

2.4.0

2.4.0.0

Tiny PHP router

  Sources   Download

Apache-2.0

The Development Requires

by Krzysztof Magosa

routing router tiny

09/10 2014

2.3.0

2.3.0.0

Tiny PHP router

  Sources   Download

Apache-2.0

The Development Requires

by Krzysztof Magosa

routing router tiny

09/10 2014

2.2.0

2.2.0.0

Tiny PHP router

  Sources   Download

Apache-2.0

The Development Requires

by Krzysztof Magosa

routing router tiny

08/10 2014

dev-caching

dev-caching

Tiny PHP router

  Sources   Download

Apache-2.0

The Development Requires

by Krzysztof Magosa

routing router tiny

08/10 2014

2.1.2

2.1.2.0

Tiny PHP router

  Sources   Download

Apache-2.0

The Development Requires

by Krzysztof Magosa

routing router tiny

08/10 2014

2.1.1

2.1.1.0

Tiny PHP router

  Sources   Download

Apache-2.0

The Development Requires

by Krzysztof Magosa

routing router tiny

08/10 2014

2.1.0

2.1.0.0

Tiny PHP router

  Sources   Download

Apache-2.0

The Development Requires

by Krzysztof Magosa

routing router tiny

08/10 2014

2.0.0

2.0.0.0

Tiny PHP router

  Sources   Download

Apache-2.0

The Development Requires

by Krzysztof Magosa

routing router tiny

07/10 2014

1.1.2

1.1.2.0

Tiny PHP router

  Sources   Download

Apache-2.0

The Development Requires

by Krzysztof Magosa

routing router tiny

07/10 2014

dev-test

dev-test

Tiny PHP router

  Sources   Download

Apache-2.0

The Development Requires

by Krzysztof Magosa

routing router tiny

07/10 2014

1.1.1

1.1.1.0

Tiny PHP router

  Sources   Download

Apache-2.0

The Development Requires

by Krzysztof Magosa

routing router tiny

07/10 2014

1.1.0

1.1.0.0

Tiny PHP router

  Sources   Download

Apache-2.0

The Development Requires

by Krzysztof Magosa

routing router tiny

05/10 2014

1.0.0

1.0.0.0

Tiny PHP router

  Sources   Download

Apache-2.0

The Development Requires

by Krzysztof Magosa

routing router tiny

05/10 2014

0.2.2

0.2.2.0

Tiny PHP router

  Sources   Download

Apache-2.0

The Development Requires

by Krzysztof Magosa

routing router tiny

04/10 2014

0.2.1

0.2.1.0

Tiny PHP router

  Sources   Download

Apache-2.0

The Development Requires

by Krzysztof Magosa

routing router tiny

04/10 2014

0.2.0

0.2.0.0

Tiny PHP router

  Sources   Download

Apache-2.0

The Development Requires

by Krzysztof Magosa

routing router tiny

04/10 2014

0.1.0

0.1.0.0

Tiny PHP router

  Sources   Download

Apache-2.0

The Development Requires

by Krzysztof Magosa

routing router tiny