PHPackages                             webapper/silexi18nroutes - 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. [Localization &amp; i18n](/categories/localization)
4. /
5. webapper/silexi18nroutes

ActiveLibrary[Localization &amp; i18n](/categories/localization)

webapper/silexi18nroutes
========================

A simple Silex service provider for declaring multi-lingual (I18N) routes

018PHP

Since Dec 10Pushed 10y ago1 watchersCompare

[ Source](https://github.com/Webapper/silexi18nroutes)[ Packagist](https://packagist.org/packages/webapper/silexi18nroutes)[ RSS](/packages/webapper-silexi18nroutes/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

silexI18Nroutes
===============

[](#silexi18nroutes)

A simple Silex service provider for declaring multi-lingual (I18N) routes

Usage
-----

[](#usage)

First: register the provider

```
...
$app->register(new \Sir\SirServiceProvider($app));
...
```

You can define multiple route configurations loader. For example:

```
...
// SIR registered before
...
$routesData = json_decode(file_get_contents('routes.json'), true);
$app['sir']->add(new \Sir\ArrayLoader($routesData));
$app['sir']->add(new \Sir\CallbackLoader(function($language) use ($app) {
	if (!isset($app['i18nroutes'][$language])) throw new \Exception();
	return $app['i18nroutes'][$language];
});
...
$app['sir']->loadRoutes('en');
$app->mount($app['sir']->getNodeRoute('blog'), \Acme\Controllers\BlogActions::init($app));
$app->mount($app['sir']->getNodeRoute('pagecontent'), \Acme\Controllers\PageActions::init($app));
...
```

A possible code for blog controller collection is (`/Controllers/BlogActions.php`):

```
namespace Acme\Controllers

use Silex\Application;
use Silex\ControllerCollection;

class BlogActions {
	/**
	 * @param Application $app
	 * @return ControllerCollection
	 */
	public static function init(Application $app) {
		// loads all routes defined for "blog" node, used classes and defined actions must be exists
		$controllers = $app['sir']->registerRoutes('blog');
		return $controllers;
	}
}
```

Example contents of `routes.json` file:

```
{
	'en': {
		'blog': {
			'path': '/blog',
			'controller': 'Acme\\Controllers\\Blog\\',
			'routes': [{
				'path': '/post/{id}',
				'action': 'PostsAction::get',
				'assert': {'id': '\\d+'},
				'bind': 'blog_get_post'
			}, {
				'method': 'POST',
				'path': '/post/{id}',
				'action': 'PostsAction::post',
				'assert': {'id': '\\d+'}
			}, {
				'path': '/',
				'action': 'PostsAction::list'
			}]
		},
		'pagecontent': {
			'path': '/page',
			'controller': 'Acme\\Controllers\\Page\\',
			'default_resolver': 'PagesAction::get',
			'routes': [{
				'path': '/{path}'
			}, {
				'path': '/{path}',
				'method': 'POST',
				'action': 'PagesAction::post'
			}
		}
	},
	'hu': {
		'blog': {
			'path': '/naplóm',
			'controller': 'Acme\\Controllers\\Blog\\',
			'routes': [{
				'path': '/bejegyzés/{id}',
				'action': 'PostsAction::get',
				'assert': {'id': '\\d+'},
				'bind': 'blog_get_post'
		}, {
				'method': 'POST',
				'path': '/bejegyzés/{id}',
				'action': 'PostsAction::post',
				'assert': {'id': '\\d+'}
			}, {
				'path': '/',
				'action': 'GetList'
			}]
		},
		'pagecontent': {
			'path': '/oldal',
			'controller': 'Acme\\Controllers\\Page\\',
			'default_resolver': 'PagesAction::get',
			'routes': [{
				'path': '/{path}'
			}, {
				'path': '/{path}',
				'method': 'POST',
				'action': 'PagesAction::post'
			}
		}
	}
}
```

Possible route items options
----------------------------

[](#possible-route-items-options)

- `path`: The path used for the route. This is relative to the parent node's path.
- `method`: One or more HTTP methods, separated by `|`, to register the route for.
- `action`: Named action method (or in "class::method"-form) which would be executed on this route.
- `convert`: Array of argument converters - see:
- `assert`: Array of argument assertions - see:
- `defaults`: Array of argument default values - see:
- `bind`: Name of route - see:
- `https`: Define as `true` if want to require HTTPS to be used, `false` to deny HTTPS

Getting more help on handling I18N routes
-----------------------------------------

[](#getting-more-help-on-handling-i18n-routes)

By using `Sir\SirTrait` trait in your `Application` you can get help for multi-lingual content redirection when switching between languages.

### Initialize the trait

[](#initialize-the-trait)

You must be use the `before` middleware of Silex for getting use the helper methods:

```
...
// instancing your Silex application, etc.
...
$app->before(function (\Symfony\Component\HttpFoundation\Request $request) use ($app) {
	$app->sirBeforeMiddleware(); // storing important routing data in session
});
$app->initRedirector(); // adding redirect controller used by redirectSir() method
...
```

Now you can get the most important routing information whenever and wherever you want by calling `getSirRoute()`:

```
...
$routeData = $app->getSirRoute();
/*
	Could contains something like:
	- name: blog_get_post
	- args:
		- id: 42
	- get:
		- highlight: "searched,keywords,list"
*/
...
```

...and can use `redirectSir()` for generating a redirection response to switching locale and redirect to the locale-equivalent of current route:

```
...
// any controller code
...
return $app->redirectSir('hu'); // This will be changes current locale to "hu" and redirects the user agent
...
```

The method `redirectSir()` has 3 more arguments detailed below:

- `$routeName`: for defining a different route than the current one
- `$args`: for defining different route arguments than the currents are
- `$get`: for defining different HTTP GET parameters than the currents are

On redirection, SIR adding an `X-Sir-Redirection` HTTP header with value of `1` and `_locale_switched_from` GET parameter to the response, this could be checked on redirected target to be getting informed about the reason of redirection.

Suggestions for usage
---------------------

[](#suggestions-for-usage)

`\Sir\ArrayLoader` could be used best for loading static routes from configuration files, while `\Sir\CallbackLoader` could be better for use as dynamic contents route provider eg. from database storage using any ORM or pure PHP to access to it.

There is an easy way for defining custom loader classes by implementing `\Sir\LoaderInterface`.

Feedback notice
===============

[](#feedback-notice)

This is a flexible but very light-weight solution for handling multi-lingual routes, not a space-ship. Please, consider use it as is.

Any constructive feedbacks, suggestions/contributing kindly welcomed by Assarte (the author)!

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/a84c693d6fcce961c9953932737eafb748c6db09d1a5ae5e35a5eb89fd037629?d=identicon)[assarte](/maintainers/assarte)

---

Top Contributors

[![assarte](https://avatars.githubusercontent.com/u/5679362?v=4)](https://github.com/assarte "assarte (4 commits)")

### Embed Badge

![Health badge](/badges/webapper-silexi18nroutes/health.svg)

```
[![Health](https://phpackages.com/badges/webapper-silexi18nroutes/health.svg)](https://phpackages.com/packages/webapper-silexi18nroutes)
```

###  Alternatives

[symfony/translation

Provides tools to internationalize your application

6.6k836.5M2.0k](/packages/symfony-translation)[nesbot/carbon

An API extension for DateTime that supports 281 different languages.

169661.4M4.8k](/packages/nesbot-carbon)[joedixon/laravel-translation

A tool for managing all of your Laravel translations

717911.4k11](/packages/joedixon-laravel-translation)[illuminate/translation

The Illuminate Translation package.

6936.4M491](/packages/illuminate-translation)[lajax/yii2-translate-manager

Translation management extension for Yii 2

227578.8k13](/packages/lajax-yii2-translate-manager)[larswiegers/laravel-translations-checker

Make sure your laravel translations are checked and are included in all languages.

256423.2k2](/packages/larswiegers-laravel-translations-checker)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
