4

我希望自己实现某种cms。我看到我可以捕获 Apache 环境变量,称为 PATH_INFO,因此我可以在我的网站上制作动态部分(就像 joomla 一样)。

例如:

stackoverflow.com/index.php/section1/article22

目前我正在开发一个功能来找出请求的部分和文章,通过这样做:

$url_seccion = $_SERVER['PATH_INFO'];
$secciones_array =  array_values(array_filter(explode('/', $url_seccion)));

这部分工作正常,我遇到的问题是我现在拥有的所有相对路径都被破坏了。有人可以解释一下为什么会发生这种情况,我能做些什么来解决它?(请不要告诉我我必须使用绝对路径......)

4

1 回答 1

1

例子:

$url = "stackoverflow.com/index.php/section1/article22";
$myArray = array_slice( explode('/', $url), 2 );
echo "section: ". $myArray[0] ."<br /> article: ". $myArray[1]."<br />";
if(isset($myArray[0]))  {
$section = $myArray[0];
} else {
$section = "";
}

if(isset($myArray[1]))  {
$article = $myArray[1];
} else {
$article = "";
}

switch(strtolower($section)) {
default:
echo "home";
break;
case 'section1':
echo "function for find and show my article: ". $article;
break;
}

要使用保存在 db 中的部分,您可以使用 select 找到它的 id,然后是 Article

要将所有请求重定向到 index.php,请使用:

.htaccess

Options -Indexes +FollowSymLinks
RewriteEngine On
RewriteBase /

RewriteCond $0 !^(index\.php|css|js|img)
RewriteRule ^(.*)$ index.php [L]

索引.php:

$url = addslashes($_SERVER['REQUEST_URI']);
$myArray = array_slice( explode('/', $url), 1 );
于 2016-01-17T13:49:54.493 回答