【发布时间】:2014-01-29 03:40:22
【问题描述】:
我想在 PHP 应用程序中更改我的 URL,以便它使用友好的 URL。
如果我启动它,url = localhost/Inhaalopdracht/MEOK1/MVCthuis/boeken/index.php?author=1
我希望它类似于:localhost/Inhaalopdracht/MEOK1/MVCthuis/boeken/index.php/author/1
这是我的索引:
<?php
//lees de controller
require_once("controller/Controller.php");
//start de controller
$controller = new Controller();
$controller->start();
?>
我的控制器:
<?php
require_once("model/Model.php");
class Controller
{
public $model;
public function __construct()
{
$this->model = new Model();
}
public function start()
{
if(!isset($_GET['book']) && !isset($_GET['author']))
{
$books = $this->model->getBookList();
include 'view/booklist.php';
}
else if(isset($_GET['book']))
{
$book = $this->model->getBook($_GET['book']);
include 'view/viewbook.php';
}
else if(isset($_GET['author']))
{
$author = $this->model->getAuthor($_GET['author']);
$books = $this->model->getBookList();
include 'view/viewauthor.php';
}
}
}
?>
还有一个带有信息的页面的示例:
<html>
<head>
<title>De boekenlijst </title>
</head>
<body>
<table>
<tr>
<th> Titel </th>
<th> Schrijver </th>
<th> ISBN </th>
</tr>
<?php
foreach ($books as $key => $book)
{
echo '<tr>';
echo '<td><a href="index.php?book=' . $book->id . '">' . $book->title . '</a></td>';
echo '<td><a href="index.php?author=' . $book->id . '">' . $book->author . '</a></td>';
echo '<td>' . $book->isbn . '</td>';
echo '</tr>';
}
?>
</table>
</body>
</html>
【问题讨论】:
-
了解 url 重写 (mod_rewrite)
-
这与MVC无关。
-
你可以用 htaccess 做到这一点
标签: php url mod-rewrite