内容系统中的硬编码 URL、嵌套集、组合

发布于 2024-12-06 20:54:58 字数 1671 浏览 0 评论 0原文

我一直在构建一个数据库来处理为网站生成的内容,但是,从长远来看,我不确定我是否拥有最好的系统。

目前,我使用的路由方法是通过 index.php 传递所有内容,其中 .htaccess 路由如下 index.php?route=example/url (用户看到 http://www.domain.com/example/url

目前数据库的设置如下:

uid            | title        | content | category
--------------------------------------------------
/              | Home         | aaa     | 1
/example       | Example      | bbb     | 2
/example/url   | Example: URL | ccc     | 2

虽然我不确定这是否是最好的方法,特别是如果我想要将 example 重命名为 something - 我必须重命名每个 URL...

所以我还考虑了 Nested Set 方法(例如 http://www.phpclasses.org/package/2547-PHP-Manipulate-database-records-in-hierarchical-trees.html)虽然这只会显示数据库中的许多不同数字,其中我可以通过它的节点访问所有内容。下面的例子;

node | left | right | name
--------------------------
1    | 1    | 6     | Home
2    | 2    | 5     | Example
3    | 3    | 4     | URL

那么我可以使用节点作为 uid 吗?但我不确定如何将 http://www.domain.com/example/url 转换为等于 3 的 uid...

我已经有一个类别目前我的数据库中的列,对内容进行分类,尽管我可能会改变这一点。

我基本上是在寻找有关如何继续的建议,因为随着网站获得更多内容,更改设置将变得更加困难 - 所以我希望理想地从第一天起就做到这一点。

  • 两者中哪一个的可扩展性更好?

  • 如果是第二种,如何将URL转换为节点?

  • 我能否以某种方式将两者结合起来,以便原始数据库将 uid 存储为节点号,然后进行某种连接以使 uid 成为 url(如 1 中所示) - 然后]

    ^ 我想我更喜欢这个(第三个),但不确定如何在 MySQL 中准确执行,还有一些其他好处:

    • 我可以用父节点替换我的类别系统 - 这可能会更好
    • 理论上我还可以将节点 ID 存储在统计系统中,而不是 URL

如果有人可以给一些帮助/建议 - 我将不胜感激!

I've been putting together a database to handle content produced for a site, however, thinking about the long-term, I'm unsure if I have the best system.

At present I'm using the routing method of passing everything via index.php which .htaccess routes as follows index.php?route=example/url (user sees http://www.domain.com/example/url)

At present the database is setup like below:

uid            | title        | content | category
--------------------------------------------------
/              | Home         | aaa     | 1
/example       | Example      | bbb     | 2
/example/url   | Example: URL | ccc     | 2

Though I am not sure if this is the best approach, especially if I wanted to rename example to something - I'd have to rename each URL...

So I've also thought about the Nested Set method (such as http://www.phpclasses.org/package/2547-PHP-Manipulate-database-records-in-hierarchical-trees.html) though this would just show lots of different numbers in the database where I could access everything by it's node. Example below;

node | left | right | name
--------------------------
1    | 1    | 6     | Home
2    | 2    | 5     | Example
3    | 3    | 4     | URL

Then I could use the node as the uid? But I'm unsure how I could translate http://www.domain.com/example/url to the uid equalling 3...

I already do have a category column in my database at the moment, to categorise the content, though I could potentially alter this.

I'm basically looking for suggestions about how to proceed, because as the site gets more content it will be harder to change the setup - so I want to ideally get this right from day one.

  • Which of the two is better for scalability?

  • If the second, how to translate the URL to the node?

  • Could I somehow combine both so that the original database stores the uid as the node number, then do a join of some sort to make the uid be a url (as in 1) - then ]

    ^ I think I'd prefer this (the third), but unsure how to do in MySQL exactly, with some other benefits:

    • I could replace my category system with the parent node - which may be better
    • I could also then in theory store the node ID within a stats system, rather than a URL

If anyone can give some help/suggestions - I'd be grateful!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜雨飘雪 2024-12-13 20:54:58

好吧,如果你使用 index.php?route=example/url,你总是可以这样做:

$args = explode( '/', $_GET['route'] );
$args = filter_var_array( $_GET['route'], FILTER_SANITIZE_STRING );

那么你的 $args 值将是:

0 -> example
1 -> url

等等。然后你可以使用这些值来确定什么要加载的模板,以及从数据库中获取哪些内容,或者您​​已经在做的其他事情。

HTH。

Well, if you use index.php?route=example/url, you could always do something like this:

$args = explode( '/', $_GET['route'] );
$args = filter_var_array( $_GET['route'], FILTER_SANITIZE_STRING );

Then your values of $args would be:

0 -> example
1 -> url

etc. You could then use these values to determine what template to load, and what content to grab from the database, or whatever else you're doing already.

HTH.

寒冷纷飞旳雪 2024-12-13 20:54:58

嵌套集合模型在这里可能是一个不错的选择。这将导致表布局如下(id,left,right 是嵌套集模型所需的字段,其他字段包含相应的内容):

| id | left | right | uid | title | content | category |

有关如何执行特定查询的更多详细信息,请参阅可以找到 这里

但是,我不会在数据库上执行查找,而是在一个简单的数组缓存上执行:

new array('/' => array('content' => 'aaa', 'category' => 'bbbb'),
          '/example/' => array(),
          .....
);

该缓存可以非常容易地构建(尽管昂贵)并且查询也非常容易。

附带说明:我怀疑您正在尝试在这里对页面内容进行建模。也许您应该重构数据库结构,因为该表有两个职责(url->内容映射和内容)。

The nested set model probably is a good choice here. That'd result in a table layout like (id,left,right are the fields required by the nested set model, the others contain the respective content):

| id | left | right | uid | title | content | category |

More details on how to perform a particular query can be found here.

However I would not perform the look up on the database but a simple array cache:

new array('/' => array('content' => 'aaa', 'category' => 'bbbb'),
          '/example/' => array(),
          .....
);

This cache can be build up very easy (though expensive) and queried very easy.

On a side note: i suspect you're trying to model page content here. Maybe you should refactor you database structure then as this table would have two responsibilities (url->content mapping and content).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文