webapper/silexi18nroutes

一个简单的Silex服务提供程序,用于声明多语言(I18N)路由

dev-master / 1.0.x-dev 2015-12-10 12:17 UTC

This package is not auto-updated.

Last update: 2024-09-14 17:53:35 UTC


README

一个简单的Silex服务提供程序,用于声明多语言(I18N)路由

使用方法

首先:注册提供程序

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

您可以定义多个路由配置加载器。例如

...
// 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));
...

博客控制器集合的可能的代码是 (<...>/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;
	}
}

routes.json 文件的可能内容

{
	'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'
			}
		}
	}
}

可能的路由项选项

获取更多关于处理I18N路由的帮助

通过在您的Application中使用Sir\SirTrait特质,您可以在语言切换时获得多语言内容重定向的帮助。

初始化特质

您必须使用Silex的before中间件来使用辅助方法

...
// 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
...

现在,您可以通过调用getSirRoute()在任何时候和任何地方获取最重要的路由信息

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

...并且可以使用redirectSir()生成一个用于切换区域并重定向到当前路由区域等价物的重定向响应

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

redirectSir()方法有3个更详细的参数

  • $routeName:用于定义不同于当前的路由
  • $args:用于定义不同于当前的路由参数
  • $get:用于定义不同于当前的HTTP GET参数

在重定向时,SIR会在响应中添加一个值为1X-Sir-Redirection HTTP头和一个_locale_switched_from GET参数,这可以在重定向目标中进行检查,以了解重定向的原因。

使用建议

\Sir\ArrayLoader最好用于从配置文件中加载静态路由,而\Sir\CallbackLoader则更适合用作动态内容路由提供程序,例如从数据库存储中使用任何ORM或纯PHP访问。

可以通过实现\Sir\LoaderInterface来轻松定义自定义加载器类。

反馈通知

这是一个灵活但非常轻量级的处理多语言路由的解决方案,而不是一艘宇宙飞船。请将其按原样使用。

任何建设性的反馈、建议/贡献都受到作者Assarte的热烈欢迎!