承接 webapper/silexi18nroutes 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

webapper/silexi18nroutes

Composer 安装命令:

composer require webapper/silexi18nroutes

包简介

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

README 文档

README

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

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

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

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

\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

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)!

webapper/silexi18nroutes 适用场景与选型建议

webapper/silexi18nroutes 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 18 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 08 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 webapper/silexi18nroutes 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 webapper/silexi18nroutes 我们能提供哪些服务?
定制开发 / 二次开发

基于 webapper/silexi18nroutes 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 18
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 14
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: CC-4.0
  • 更新时间: 2015-08-14