我有点迷茫,但据我了解,您的问题是:如何在 url 中传递参数,以便当我转到 URL http://www.example.com/username/albums时,它会转到相册,对吗?
这是一个类似的例子:
if (isset($_GET['page'])) {
$pageArgs = explode('/', $_GET['page']);
if(isset($pageArgs[0])) {
$username = $pageArgs[0];
} else {
header('Location: index.php');
exit();
}
$action = (isset($pageArgs[1])) ? $pageArgs[1] : false;
switch($action)
{
case 'albums':
print 'These are my albums <br/>';
break;
case 'music':
print 'This is my music! <br/>';
break;
default:
print '
<nav>
<ul>
<li><a href="'.$username.'/albums">Albums</a></li>
<li><a href="'.$username.'/music">Music</a></li>
</ul>
</nav>';
break;
}
}
根据您的需要调整它...
注意:请注意,我将参数更改为页面而不是用户名,因此您必须更新重写 mod 规则。我还使用 / 来分隔页面“参数”。您可以使用 ”?” (我想,不确定)如果你喜欢......
这遵循一个简单的 MVC
索引.php
// Find which page is requested
$pageIDs = pageSelector();
// namespaced pages. You can change to real namespaces with:
// $mPage = '\\Page\\' . $pageIDs[0];
$mPage = 'Page_' . $pageIDs[0];
array_shift($pageIDs);
// Path to Template file (the static elements common to all pages like
// header, footer, logo, navigation, etc...
$templatePath = 'templates/my_template.html';
if(!empty($pageIDs))
$pageArgs = $pageIDs;
else
$pageArgs = false;
//create Page Object, the dynamic content divided by sections
//for instance, content, columnA, columnB
$page = new $mPage($pageArgs);
$template = new Template($templatePath, $page);
//Here's an example of a section
$template->addSection('subNavigation');
print $template;
// PageSelector Function
function pageSelector()
{
if (isset($_GET['page'])) {
$pIDs = explode('/', $_GET['page']);
} else {
$pIDs = array('home');
}
$pagePath = 'pages/' . $pIDs[0] . '.php';
if (file_exists($pagePath)) {
require_once($pagePath);
return $pIDs;
} else {
$pagePath = 'pages/home.php';
require_once($pagePath);
return array('home');
}
}
//Template Object
class Template
{
private $path, $page;
private $sections = array('content');
public function __construct($tPath, $page)
{
$this->path = $tPath;
$this->page = $page;
}
public function addSection($newSectionName)
{
$this->sections[] = $newSectionName;
}
public function __toString()
{
$html = file_get_contents($this->path);
foreach($this->sections as $section) {
$html = str_replace('[%%'.$section.'%%]', $this->page->show($section), $html);
}
return $html;
}
}
文件页面/home.php
class Page_home
{
public function __construct($args) {}
public function show($section)
{
switch($section)
{
case 'content':
return 'this is my main page';
break;
default:
return '';
break;
}
}
}
文件页面/user.php
class Page_user
{
private $subPage;
public function __construct($args)
{
if(isset($args[0]))
$this->username = $args[0];
else {
$this->username = false;
return;
}
if(isset($args[1]))
$this->subPage = $args[1];
else {
$this->subPage = false;
return;
}
}
public function show($section)
{
if($this->username)
{
switch($section)
{
case 'content':
$otp = 'My username is ' . $this->username . '<br/>';
$otp .= $this->showSubPage();
return $otp;
break;
case 'subNavigation':
return $this->nav();
break;
default:
return '';
break;
}
} else {
return 'username not found!';
}
}
protected function nav()
{
return
'<nav>
<ul>
<li><a href="user/'.$this->username.'/albums">Albums</a></li>
<li><a href="user/'.$this->username.'/music">Music</a></li>
</ul>
</nav>';
}
protected function showSubPage()
{
switch($this->subPage)
{
case 'albums':
return 'These are my albums <br/>';
break;
case 'music':
return 'This is my music! <br/>';
break;
}
}
}
模板/my_template.html
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<div id="mainNav">myLink | Go | Here</div>
<nav id="subNav">[%%subNavigation%%]</nav>
<div id="content">[%%content%%]</div>
<footer>my (c) copyright notice</footer>
</body>
</html>