定制锂路由方案(Custom lithium routing scenario)

编程入门 行业动态 更新时间:2024-10-26 18:29:20
定制锂路由方案(Custom lithium routing scenario)

我的任务是用现有的大型链接目录重写现有网站。 为了论证,让我们假设我们不能做任何会改变链接目录的事情。 以下是我们正在使用的链接结构的几个示例:

项目页面将是:

www.domain.com/widgets/some-totally-awesome-large-purple-widget

类别子页面页面将是:

www.domain.com/widgets/purple-widgets

类别父页面页面将是:

www.domain.com/widgets/

自定义页面可能是:

www.domain.com/some-random-page

各种页面类型太多,无法编写单个路由器。

使用Router :: connect我可以轻松地使用以下内容来解释第一个和第二个场景:

Router::connect('/{:pageroot}/{:pagekey}', 'Pages::index');

反过来,Pages :: index方法使用'/ widgets / purple-widgets'的“键”查找数据库中的条目。

但是,框架默认为第三和第四页的'/ {:controller} / {:action} / {:args}'路由。 我知道这是框架的正确行为。 此外,最佳实践将声明我应该编写网站以匹配此行为。 但是,这不是一个选择。

我需要的是一个路由器,它允许第三个和第四个例子的功能与第一个相同。 应将所有示例发送到Pages :: index控制器,后者又使用URL路径作为键来查询数据库。

I've been tasked with rewriting an existing website with large pre-existing link catalog. For argument's sake, let's assume we can't do anything that would change the link catalog. Here's a few examples of the link structure we're working with:

An item page would be:

www.domain.com/widgets/some-totally-awesome-large-purple-widget

A category sub page page would be:

www.domain.com/widgets/purple-widgets

A category parent page page would be:

www.domain.com/widgets/

A custom page may be:

www.domain.com/some-random-page

The various page types are too numerous to write individual Routers for.

Using Router::connect I can easily account for the first and second scenarios using something like:

Router::connect('/{:pageroot}/{:pagekey}', 'Pages::index');

In turn, the Pages::index method looks for entries in our database with the "key" of '/widgets/purple-widgets'.

However, the framework defaults to the '/{:controller}/{:action}/{:args}' route for pages like the third and fourth. I know that this is the correct behavior for the framework. Also, best practice would state that I should write the site to match this behavior. But, that isn't an option here.

What I need is a Router that would allow the third and fourth examples to function the same as the first. All examples should be sent to the Pages::index controller, which in turn queries a database using the URL path as a key.

最满意答案

如果您在URL中没有任何约定,那么在页面,项目和类别之间是什么。 我会选择一个非常通用的路由器。

Router::connect('/{:category}/{:page}/{:item}', 'Pages::any'); Router::connect('/{:category}/{:page}', array('Pages::any', 'item' => null)); Router::connect('/{:category}', array('Pages::any', 'page' => null, 'item' => null));

并在Pages::any()中搜索正确的东西。 那个category毕竟是一个page (例子4)? 该page是一个item (例1)吗?

要么

您将URL存储在某处(例如,数据库中的映射表)并使用锂路由的pattern版本。

Router::connect(new Route(array( 'pattern' => '@^/(?<path>.+)$@', 'params' => array('controller' => 'pages', 'action' => 'any'), 'keys' => array('path' => 'path'), // extra stuff, if the path is `tata`, it skips this route and uses // any of the following ones that matches. 'handler' => function($request) { if ($request->params['path'] == 'tata') { return false; } else { return $request; } } )));

从那时起,您将获得完整的URL。

If you don't have any convention in the URL for what is what, between page, item and category. I'd go with a very generic router.

Router::connect('/{:category}/{:page}/{:item}', 'Pages::any'); Router::connect('/{:category}/{:page}', array('Pages::any', 'item' => null)); Router::connect('/{:category}', array('Pages::any', 'page' => null, 'item' => null));

And in Pages::any() to search for the correct stuff. Is that category a page after all (example 4)? Is that page an item (example 1)?

or

You store the URL somewhere (e.g. a mapping table in the database) and use the pattern version of a lithium Route.

Router::connect(new Route(array( 'pattern' => '@^/(?<path>.+)$@', 'params' => array('controller' => 'pages', 'action' => 'any'), 'keys' => array('path' => 'path'), // extra stuff, if the path is `tata`, it skips this route and uses // any of the following ones that matches. 'handler' => function($request) { if ($request->params['path'] == 'tata') { return false; } else { return $request; } } )));

From that point, you'll get the full URL.

更多推荐

本文发布于:2023-08-07 14:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464697.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由   方案   Custom   scenario   routing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!