PHPackages                             coercive/router - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. coercive/router

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

coercive/router
===============

Coercive Utility Multilingual Yaml Router

3.4.6(2mo ago)11.8kMITPHPPHP &gt;=7.4

Since Dec 4Pushed 2mo ago2 watchersCompare

[ Source](https://github.com/Coercive/Router)[ Packagist](https://packagist.org/packages/coercive/router)[ Docs](http://coercive.fr)[ RSS](/packages/coercive-router/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (10)Dependencies (3)Versions (59)Used By (0)

Coercive Utility Router
=======================

[](#coercive-utility-router)

A simple and multilingual yaml router.

Get
---

[](#get)

```
composer require coercive/router

```

Example YAML
------------

[](#example-yaml)

```
INDEX:
    __: Projet\Controller::Method
    FR: /
    EN: /

HOME:
    __: Projet\Controller::Method
    FR: accueil
    EN: home

###########################

# {slug} can be all exept /
# {nb} integer (three numbers) (custom regex)
# {test} is optional (because brackets []) and like @abcdef ...

BLOG:
    __: Projet\Controller::Method
    FR: /{slug}/article-{nb:[0-9]{3}}[/{test:@[a-z]+}]
    EN: /{slug}/post-{nb:[0-9]{3}}[/{test:@[a-z]+}]
    options:
        example: 'Hello World.'

###########################

# {id} integer (two numbers) (custom regex)
# {name} can be all exept /
# {script} can be all exept /

COMPLEX:
    __: Projet\Controller::Method
    FR: chiffre-{id:[0-9]{2}}/nom-{name}/mechant-{script}
    EN: number-{id:[0-9]{2}}/name-{name}/bad-{script}

###########################

# Multi optional inner same brackets !
# {date} integer (optional)
# {slug} can be all exept / (optional too)

MULTI:
    __: Projet\Controller::Method
    FR: fr/multi-optional[/{date:\d+}-{slug}]
    EN: en/multi-optional[/{date:\d+}-{slug}]
```

The "\_\_" (double underscore) is what the router return when the route match. You can set what information you want, basicaly the item to load. In this example the router return a namespace / controller / method to launch.

Load
----

[](#load)

```
use Coercive\Utility\Router\Loader;

# YAML

# Load one routes file
$router = Loader::loadByYaml('/path/routes.yml')
# OR multi-files
$router = Loader::loadByYaml(['/path/first_routes.yml', '/path/second_routes.yml', ...])

# JSON

# Load one routes file
$router = Loader::loadByJson('/path/routes.json')
# OR multi-files
$router = Loader::loadByJson(['/path/first_routes.json', '/path/second_routes.json', ...])

# ARRAY

# Load routes array
$router = Loader::loadByArray([
    INDEX => [
        '__' => 'Projet\Controller::Method',
        'FR' => '/',
        'EN' => '/'
    ],
    HOME => [
            '__' => 'Projet\Controller::Method',
            'FR' => 'accueil',
            'EN' => 'home'
    ],
    [ ... ]
])

# CACHE

# You can cache the prepared routes and re-inject them after
# For example, you have cache in var $cache

if( $cache->isCacheReady() ) {
    # YES
    # Load by cache
    $data = $cache->get()
    $router = Loader::loadByCache( $data )
}
else {
    # NO
    # Classical Load
    $router = Loader::loadByYaml( '/path/routes.yml' )
}

# /!\ Important
# On a previous page you have cached the prepared routes
$cache->set( $router->getPreparedRoutesForCache() )

# The second param in loader allow you to set a basepath before your routes
# You can now use the router in local mode or ip with subdirectories before
# Example : 000.000.00.00/folder/subfolder/my-custom-route/my-param
$router = Loader::loadByArray([...], '/folder/subfolder')

# If you don't use the loader :
$parser->setBasePath('/folder/subfolder')

# OPTIONAL CONFIG PREFIX for loadByYaml or loadByJson
# (if use loadByArray : you have to set it yourself)
$router = Loader::loadByYaml([
	'/path/routes.yml',
	'SHOP_' => '/other-directory/shop/path/routes.yml',
	'BLOG_' => '/other-directory/blog/path/routes.yml'
]);
# (the first route has no prefix, the others ID : SHOP_... and BLOG_...)
```

Start the router
----------------

[](#start-the-router)

When config is loaded, start the router if you wan't to handle current route.

```
