php - 'router' - 如何使其正确?

标签 php mysql foreign-keys routes url-routing

我想问你路由器的最佳解决方案。

我的数据库中有一些表(文章、页面、类别等),它们都有 ID。 我想为他们每个人生成特定的 URL,例如。 /en/article/25-This-is-an-test-article.

但是如何摆脱那个/article/page,...?用户不希望看到它 - 它只会增加 url 的长度...

我的第一个想法是简单的路由表:

CREATE TABLE `router` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `type` varchar(50) NOT NULL,
  `entity_id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB;

但是有一个致命的问题——无法为 enitity_id 列设置外键,因为它应该引用更多的列,而这在 SQL 中是不可能的。

我的问题是: 你认为我应该坚持那个解决方案吗(忽略外键) 或者我应该忘记它并在 URL 中附加类型?

最佳答案

I want /en/article/55-something to become /en/55-something - with the best performance, with the best and cleanest way (the problem is WHERE and HOW to save the info that 55-something IS an article, not the page)

该信息的最佳位置就在 URL 中,这样您就不必为脆弱的、最佳猜测的查找而烦恼。

简短回答:您已经正确地做到了。

关于php - 'router' - 如何使其正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7047623/

相关文章:

php - 取模PHP问题

directory - '覆盖' php.ini 设置

php - Jquery 切换行为不一致

php - MySql 更新并将 null 设置为具有其他表外键的字段

javascript - 如何获取 JS 提示框的值,然后将其传递给 PHP 并保存在数据库中?

php - 我可以在 PHP 中混合使用 MySQL API 吗?

mysql - 在一个结果行中选择多行

mysql - 为所有具有相同列名的表添加外键

mysql - 删除带有外键的表和删除外键

mysql - Laravel 5.2 Eloquent 关系