gpt4 book ai didi

php - .htaccess 重写 Backbone 路由器转换规则

转载 作者:行者123 更新时间:2023-11-28 02:29:33 24 4
gpt4 key购买 nike

我在互联网上做了一些挖掘,但没有什么是显而易见的。

我希望将自定义 CMS 与主干集成,以便可以使用原始 http 请求或通过主干的“状态”开关加载页面。 CMS 目前专注于通过 htaccess/mod-rewrite 自动重写其路由/url 规则,但主干网对其内部路由使用不同的格式化结构。

我希望 CMS 能够将其重写规则转换为主干格式,以便每当将页面添加到 CMS 时,主干就会意识到它并自动更新自身。例如,CMS 将这样的页面负载输出到全局 SiteMap 对象:

....
"contact" => array(
"key" => "contact",
"url" => "^(en|fr|de)/contact/$",
"type" => "page",
"template" => "contact",
"method" => "contact",
"sitemap" => TRUE,
//other page config vars etc
),//..and so on
...

然后有一个解析器将规则写入 .htaccess,如下所示:

RewriteRule ^(en|fr|de)/contact$ index.php?page=contact&lang=$1&section=$2

问题:

显然是backbone router format有点不同,需要在 PHP 中完成大量翻译工作才能输出漂亮干净的路由器配置。我不敢相信我是第一个遇到这样的事情的人,因为这似乎是尝试将 Backbone 应用程序集成到 CMS 的必要步骤。谁能指出我翻译这些字符串格式的某种方法的方向?也许我处理这个问题的方式是错误的?

非常感谢。

最佳答案

Backbone 支持使用 router.route 将路由定义为正则表达式方法。 Apache mod_rewrite 规则也是正则表达式。因此,诸如 ^(en|fr|de)/contact/$ 之类的表达式已经是有效的 Backbone 路由。

您没有说明是否已经有一种机制可以通过“站点地图”服务或将数据引导到服务器上的 HTML 页面来将路由配置获取到客户端。但是,假设您可以输出这样的站点地图对象:

var sitemap = {
"contact": {
"url": "^(en|fr|de)/contact/$",
"type": "page"
//..other properties
},
//...other pages
}

您可以通过迭代站点地图来注册路线:

var CMSRouter = Backbone.Router.extend({
page: function(pageName, language) {
console.log(pageName); // -> "contact"
console.log(language); // -> "en"
}
});

var router = new CMSRouter();
_.each(sitemap, function(entry, key) {
//handler method based on type ("page")
var handler = router[entry.type];

//create a pre-applied callback function where the first argument
//is always set to the sitemap entry key ("contact"), and the
//rest of the arguments are filled from the capturing groups from
//the regular expression.
var callback = _.bind(handler, router, key);

//register route
router.route(new RegExp(entry.route), callback);
});

Backbone.history.start();

这可能没有考虑到系统的很多复杂性,但原则上它应该运行良好。如果您可以用更准确的要求编辑您的问题,我将很乐意编辑答案。

关于php - .htaccess 重写 Backbone 路由器转换规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14437488/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com