【发布时间】:2019-05-20 10:17:52
【问题描述】:
大家好,我是 PHP 中路由系统的新手。在网上搜索我发现了这个简短的代码来解释路由应该如何工作但是......我不明白它以哪种方式将我的请求路由到所需的页面:
<?php
// Get the requested path with $_SERVER['REDIRECT_URL'],
// and require the page you want to display. I have '' and '/' for both url.com/ and url.com.
// REDIRECT_URL returns normal url e.g. /review,
// in the other hand REQUEST_URI returns including query string e.g. /review?page=4
$request = $_SERVER['REDIRECT_URL'];
switch ($request) {
case '/' :
require __DIR__ . '/views/index.php';
break;
case '' :
require __DIR__ . '/views/index.php';
break;
case '/about' :
require __DIR__ . '/views/about.php';
break;
default:
require __DIR__ . '/views/404.php';
break;
}
当我第一次打开它时,它会将我重定向到 index.php:
<h1>main</h1>
另外一个页面是about.php:
<h1>about</h1>
我的问题是:如何使用路由系统切换到about.php?
因为如果我在 url localhost/simpleRouter/views/about.php 中写入,看起来我正在绕过路由系统....所以我无法弄清楚如何正确使用它在页面之间切换。 Morover,index.php 页面向我显示 MAIN,这很好,但我收到以下内容:
注意:未定义的索引:REDIRECT_URL 在 D:\App\xAMPP\htdocs\studio\Php\SviluppareInPHP7\CAP7\simpleRouter\index.php 在第 9 行
感谢您回答我的问题并帮助我提高在该领域的知识。
【问题讨论】: