Types

Route types manage the arguments in a route more flexibly by increasing the security.

$config = array(
    'types' => [
        new IntType('<int:id>'),
        new StrType('<str:name>'),
        new StrType('<str:word>'),
        new AnyType('<any:any>'),
        new BoolType('<bool:status>'),
        new IntType('<int:page>'),
        new SlugType('<slug:slug>'),
        new SlugType('<slug:slug_>', '(?<%s>[\w-_]+)'), // slug with underscore
    ]
);

Default types

Type Regex Route Php Url
AnyType (?<any>.*) http://example.com/<any:any>/ string http://example.com/%s
BoolType (?<status>[0-1]) http://example.com/<bool:status>/ boolean http://example.com/%01d
FourDigitYearType (?<year>[0-9]{4}) http://example.com/<yyyy:year>/ integer http://example.com/%04d
IntType (?<id>\d+) http://example.com/<int:id>/ integer http://example.com/%d
SlugType (?<slug>[\w-]+) http://example.com/<slug:slug>/ string http://example.com/%s
StrType (?<name>\w+) http://example.com/<str:name>/ string http://example.com/%s
TranslationType (?<locale>[a-z]{2}) http://example.com/<locale:locale>/ string http://example.com/%02s
TwoDigitDayType (?<day>[0-9]{2}) http://example.com/<dd:day>/ integer http://example.com/%02d
TwoDigitMonthType (?<month>[0-9]{2}) http://example.com/<mm:month>/ integer http://example.com/%02d

Following example shows the structure of Int type.

class IntType extends Type
{
    protected $regex = '(?<%s>\d+)';

    /**
     * Php format
     * 
     * @param  number $value 
     * @return int
     */
    public function toPhp($value)
    {
        return (int)$value;
    }

    /**
     * Url format
     * 
     * @param mixed $value
     * @return string
     */
    public function toUrl($value)
    {
        return sprintf('%d', $value);
    }
}

The second parameter of the construct method can be used to obtain a new regular expression from the defined type functions.

new SlugType('<slug:slug>');
new SlugType('<slug:slug_>', '(?<%s>[\w-_]+)');  // slug with underscore

You need to change the name of cloned type. In the above example, the slug type is cloned with the name <slug:slug_> to add underline support.