【发布时间】:2020-01-12 08:48:58
【问题描述】:
我试图搜索这个问题,但没有找到可以解决我的问题的东西 - 我几乎可以肯定我没有输入正确的搜索,因为我想这对其他人来说也是一个问题。如果我正在打败一匹死马,请指出正确的方向,谢谢。
现有代码
我正在构建一种 MVC 框架。
.htaccess 将所有请求路由到 index.php。
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php [QSA,L]
<?php
require_once "./core/init.php";
Router::create( "./core/router/routes.php" )->direct( Request::uri(), Request::method() );
我的 router.class.php 文件:
<?php
class Router {
protected $routes = array(
"GET" => array(),
"POST" => array()
);
public static function create( $routes ) {
$router = new static;
include $routes;
return $router;
}
public function get( $uri, $controller ) {
$this->routes[ "GET" ][ $uri ] = $controller;
}
public function post( $uri, $controller ) {
$this->routes[ "POST" ][ $uri ] = $controller;
}
public function direct( $uri, $method ) {
if ( array_key_exists( $uri, $this->routes[ $method ] ) ) {
include $this->routes[ $method ][ $uri ];
} else {
include $this->routes[ "GET" ][ "not-found" ];
}
}
}
路由在 routes.php 中定义,如下所示(仅显示相关路由):
$router->get( "post", "controllers/get/post.controller.php" );
我的问题
当前导航到下面显示帖子,帖子是使用 slug 从数据库中检索的。
/post?p=my-post-name
如何重写我的路由器或 .htaccess 以在以下 URL 中显示相同的帖子?
/post/my-post-name
【问题讨论】: