Skip to content
This repository has been archived by the owner on Sep 19, 2022. It is now read-only.

Latest commit

 

History

History
106 lines (89 loc) · 2.85 KB

README.md

File metadata and controls

106 lines (89 loc) · 2.85 KB

Saffron PHP Router

Build Status Scrutinizer Code Quality Coverage Status

Router made with high performance in mind. Apache 2.0 licensed.

Features

  • No dependencies
  • High performance
  • Method condition support
  • Domain condition support
  • Routes with optional parameters
  • Reverse routing

Installation

You can easily install Saffron by adding below requirement to your composer.json

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

Usage

Please follow to example to see how simply you can use the Saffron in your project :-)

Setting routes

Matching request against uri (path)

Static uri

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

Dynamic uri

$route->setUri('/contact-with/{name}');
$route->setUri('/contact-with/{name}/by/{method}');

Matching request against domain

Static domain

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

Dynamic domain

$route->setDomain('www.example.{tld}');

Matching request against http method

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

Matching request against https

// Allow only https traffic
$route->setHttps(true);

// Allow only non-https traffic
$route->setHttps(false);

// Https doesn't matter (default setting)
$route->setHttps(null);

Setting possible values for placeholders

If you want to allow only some values to be passed to placeholders, you can restrict their values using regular expressions. Saffron uses PERL compatible expressions (like in preg_match). All requirements are enclosed by ^ and $ internally by Saffron.

$route->setRequirements(
    [
        'name' => '\w+',
        'method' => 'phone|email',
        'tld' => 'com|org',
    ]
);

Setting default values for placeholders

If you want some placeholder to be optional, you can set default value for it. It makes sense only for one or more placeholders on the end of uri, or one of more on the beggining of domain.

$route->setDefaults(
    [
        'placeholder1' => 'value1',
    ]
);

Setting controller to be fired by Executor

To run controller (using Executor) when route is matched you can set class and method in it. When you omit method it takes default value 'indexAction'

$route->setTarget('HomeController');
$route->setTarget('ContactController', 'emailAction');