我正在研究一个很好的路由解决方案,并在我正在处理的社交网络类型项目中包含正确的页面。我决定通过一个入口点文件运行我的所有路由。我不希望使用现有的框架。如果您查看下面的示例代码,您会发现它非常基本,并且对于高流量站点的性能应该很好,除非我遇到需要分页的模块。这一点的重点是在网站上的任何地方都有干净的URL。
因此,如果网址中有分页号码,请帮我修改分页工作。
(ie; domain.com/blogs/comments/234)
.htaccess文件
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ test.php?uri=$1 [NC,L,QSA]
test.php文件
<?PHP
//get url from URL
$uri = isset( $_GET['uri'] ) ? $_GET['uri'] : null;
// Array of possible pages to include
$pageId = array(
'account' => 'modules/account/index.php',
'account/create' => 'modules/account/create.php',
'account/settings' => 'modules/account/settings.php',
'account/login' => 'modules/account/login.php',
//example of a URL which will need paging....
'users/online/' => 'modules/users/online.php',
'users/online/12' => 'modules/users/online.php?page=12' // not sure if including a page like this is even possible?
);
// If $_GET['uri'] exist and there is a valid key/value for it in the $pageId array then include the file
if( $uri !== null ) {
if (isset($pageId[$uri])) {
require_once $pageId[$uri];
}
else {
require_once 'home.php';
}
}
else {
require_once 'home.php';
}
?>
答案 0 :(得分:1)
我认为你在这里做了一些过早的优化。看看关于为高流量网站配置apache的quora的讨论。我想这可能会对你有所帮助。
除此之外,您可能应该从$pageId
更改key
数组的语义 - &gt;今天是page
的{{1}} - &gt; regex
数组。
类似的东西:
replacement
在你的路由代码中你应该迭代数组构建一个正则表达式并匹配url。当它匹配时你应该进行替换,然后你可以调用页面。然而,如果这是直接完成它将会破坏你的路由性能:(。很多框架已经很好地完成了我所知道的,我认为你应该试着去看看那里你可以借用:)。