我正在为我的网站寻找一个PHP路由器,我的文件在根目录中;例如:index.php,details.php,signup.php,recover.php
所以我想在名为pages的目录中找到details.php,signup.php和recover.php,然后在mysite.com/details.php上访问mysite.com/details或mysite.com/?page =详细信息...我真的想把它们放在一个不同于root的文件夹中。
谢谢大家!
答案 0 :(得分:0)
你只想要一个MVC。 我强烈建议您按照本教程系列进行操作。没有框架使用,但仍在工作。
https://www.youtube.com/watch?v=OsCTzGASImQ&list=PLfdtiltiRHWGXVHXX09fxXDi-DqInchFD
答案 1 :(得分:0)
我多年前写了一个剧本,我认为它可能很适合你的需要。
<?php
/*
---------------------------------------------------------------------------
Redirector Script
Copyright (C) 2008, methodica.ch, http://www.methodica.ch
Purpose: Redirect GET request according the pattern 'location'
Pattern variables (embraced in curly brackets):
prot: Protocol (i.e. http | https)
host: Hostname (e.g. www.example.org)
port: Portnumber (80 | 443 | 81)
path: Path to the file (includes trailing '/', might be empty)
file: Filename
quer: Query, GET parameters (e.g. 'id=myId&mode=1&close=automatic')
Other text is taken literally
---------------------------------------------------------------------------
*/
define('location','{prot}://{host}{port}/{path}pages/{file}{quer}');
// Do not modify the code below! -------------------------------------------
// Get URI elements
$prot = ((isset($_SERVER['HTTPS'])) && (strtoupper($_SERVER['HTTPS']) == 'ON')) ? 'https' : 'http';
$server_name = getenv('SERVER_NAME');
$script_url = $prot . '://' . $server_name . ':' . getenv('SERVER_PORT') . $_SERVER['PHP_SELF'];
$xres = preg_match_all('%^(http[s]{0,1})://(.*?):[\d]*?/(.*/)*(.*)$%i', $script_url, $res, PREG_PATTERN_ORDER);
if (($xres!==false) && ($xres>0)) {
// Prepare variables
$prot = $res[1];
$host = $res[2];
$path = $res[3];
$file = $res[4];
$quer = $_SERVER['QUERY_STRING'];
$xport = getenv('SERVER_PORT');
$port = ($prot=='https') ? ( ($xport=='443') ? '' : ":$xport" ) : ( ($xport=='80') ? '' : ":$xport" );
$location = location;
// Replace variable references in pattern
preg_match_all('/\{.*?\}/i', $location, $res, PREG_PATTERN_ORDER);
for ($i = 0; $i < count($res[0]); $i++) {
$varname = str_replace(array('{','}'),'',$res[0][$i]);
$srce = $res[0][$i];
$dest = $$varname;
if (is_array($dest)) $dest = ($dest[0]);
if ($srce=='{quer}') $dest = '?'.$dest;
$location = str_replace($srce,$dest,$location);
}
// Send redirection header
header("Location: $location");
exit();
} else {
// Something went awfully wrong
echo "ERROR: Cannot parse URI:<br />'$script_url'";
exit();
}
?>