當前嘗試在OOP-PHP中編寫簡單的博客cms代碼,但遇到了一些麻煩。 爲了讓用戶訪問propre頁面,我製作了一個簡單的路由器,通過簡單的preg_match來檢查請求的uri與xml路由文件。這裏是代碼:PHP/XML路由適用於一種路由,不適用於其他
在我的引導文件:
$uri = $_SERVER['REQUEST_URI'];
$router = new Router($manager, $uri);
$content = $router->route();
在我的路由器文件:
public function route()
{
$uri = $this->uri;
foreach ($this->routes as $route) {
if (preg_match('#^'.$route->getUri().'$#', $uri, $matches)) {
if (!empty($route->getParams())) {
$route->setVars($matches[1]);
}
$controllerClass = '\Controller\\'.$route->getController();
$controller = new $controllerClass($this->manager);
$action = $route->getAction().'Action';
if (!empty($route->getVars())) {
$content = $controller->$action($route->getVars());
} else {
$content = $controller->$action();
}
return $content;
} else {
return $content = header('Location: View/404.php');
}
}
}
在我的路由XML文件:
<?xml version="1.0" encoding="utf-8"?>
<routes>
<route uri="/([0-9]*)" params="page" controller="BlogController" action="index" />
<route uri="/post-([0-9]+)" params="id" controller="BlogController" action="view" />
</routes>
問題:路由即使我有分頁系統,但不適用於單個郵件查看,對於索引操作工作得很好。 Preg_Match似乎抓住了非常好的索引路線,如果括號,重定向到正確的頁面,但當我嘗試查看單個帖子,我會重定向到我的404錯誤頁面。
僅供參考,索引模板文件,所以你可以看到的鏈接是正確的:
public function display($page)
{
$display = '<h1>Liste des articles présentés sur ce blog:</h1><br />';
$posts = $this->posts;
$pages = $this->pages;
foreach ($posts as $post) {
if (strlen($post->getContent()) > 200) {
$debut = substr($post->getContent(), 0, 200);
$debut = substr($debut, 0, strrpos($debut, ' ')).'...';
$post->setContent($debut);
}
if ($post->getNbComment() == 0 || $post->getNbComment() == null) {
$comments = 'Aucun commentaire. ';
} else if ($post->getNbComment() == 1) {
$comments = 'Un commentaire. ';
} else {
$comments = $post->getNbComment().' commentaires. ';
}
$display .=
'<div class="row">
<h3 class="col-sm-8"><a href="/post-'.$post->getId().'">'.htmlspecialchars($post->getTitle()).'</a></h3>
<div class="col-sm-12">'.nl2br(htmlspecialchars($post->getContent())).'</div>
<p class="col-sm-offset-8 col-sm-4"><em>Par '.htmlspecialchars($post->getAuthor()).' le '.$post->getDate()->format('d-m-Y').'.</em> '.$comments.'</p>
</div>';
}
$display .= '<div class="btn-group" role="button">';
for ($i=1; $i <= $pages; $i++) {
$display .= '<a href="/'.$i.'" ';
if ($i == $page) {
$display .= 'class="btn btn-primary">'.$i.'</a>';
} else {
$display .= 'class="btn btn-default">'.$i.'</a>';
}
}
$display .= '</div>';
return $display;
}
我目前失去了,甚至我的橡皮鴨子不能幫我在這一個。不用說,我檢查了是否同一個問題沒有被詢問,也找不到。
謝謝你的時間,如果我犯了錯誤,請原諒我的英語。
編輯:
如問的第一個答案,這裏是$this->routes
在路由器文件中的var_dump。正如你所看到的,兩個路由都在路由器文件和xml文件中正確定義。
array (size=2)
0 =>
object(Lib\Route)[13]
private 'uri' => string '/([0-9]*)' (length=9)
private 'params' => string 'page' (length=4)
private 'controller' => string 'BlogController' (length=14)
private 'action' => string 'index' (length=5)
private 'vars' => null
1 =>
object(Lib\Route)[11]
private 'uri' => string '/post-([0-9]+)' (length=14)
private 'params' => string 'id' (length=2)
private 'controller' => string 'BlogController' (length=14)
private 'action' => string 'view' (length=4)
private 'vars' => null
再次感謝。
好主意,謝謝你的提示。不幸的是,它並沒有解決問題:( – Tiriel
你現在有什麼? – Unex
同樣的事情,即時重定向到404錯誤頁面,就好像preg_match沒有捕獲後 - ([x])模式。 – Tiriel