在PHP的MVC中,最有效的路由URL的方法是什么?

4
我正在开发一个简单的php mvc,它将做到最基本的功能,但是能够按照我需要的方式工作。这是我第一次使用mvc方法而不是过程式编程,所以我在学习中。
在开发过程中,我不小心创造了一种奇怪的风格,目前主要的.htaccess包含几乎所有物理重写,例如论坛是:
RewriteRule ^forum/([a-zA-Z0-9_]+)_([0-9]+)/$                    index.php?controller=forum&method=showThread&urlTitle=$1&threadId=$2 [L] 
RewriteRule ^forum/([a-zA-Z0-9_]+)_([0-9]+)/all/([0-9]+)$        index.php?controller=forum&action=showThread&urlTitle=$1&threadId=$2&page=$3 [L]

目前的工作方式是所有 URL 都指向 index.php,然后使用以下方式从 URL 中获取控制器和方法:

index.php

$actionName = $_GET['action'];
$controllerName = ucfirst(strtolower($_GET['type'])).'controller';

$controller = new $controllerName;
$controller->$actionName();

controller/forumcontroller.php

class forumcontroller{

    function showThread() {

        $thread = new Thread($_GET['threadId'], $_GET['uriTitle']); 
        require "templates/thread.php";
    }

但这意味着用户可以访问我不希望他们访问的位置,例如:
/public_html/templates/index.php

我认为我需要什么?

我认为主要的 .htaccess 文件应该像这样:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]

然后在 index.php 中,您可以使用类似以下的内容:

$url = explode("/", `$_SERVER['QUERY_STRING']);`

$controller = $url[0];   //Returns "forum"
$data = $url[1];         //Returns the forum title and id

但是,采用这种方法,我不明白如何仅凭数据调用控制器中的操作?

难道你不需要像这样做吗:

 if(!$data)
     $controller->loadForum();
 elseif($data)
     $controller->loadForumThread($data);

结论

我还没有完全理解如何为具有许多不同格式的 SEO 友好 URL 的网站做最佳路由,我了解 MVC 应该如何工作,但我很难掌握路由部分,而且我遇到的所有示例似乎都非常复杂!

我真的很难看出如何编写 .htaccess 和控制器来处理许多不同格式的 URL,例如:

domain.com
domain.com/uploads
domain.com/profiles/username
domain.com/messages/inbox
domain.com/messages/createnew/userId
domain.com/forum/all/2
domain.com/forum/title_1/
domain.com/forum/title_1/all/3

我发现在我不想让用户访问的文件夹中使用.htaccess可能是一个更简单的解决方案,上面的路由方法可以工作并且使我更容易控制URL重写。有人持不同意见吗? - Dan
1个回答

3
这里有一种方法,类似于您第二个.htaccess示例。
$request = explode('/', substr($_SERVER['REQUEST_URI'], 1));
// Clean request array of empty elements
foreach($request as $k => $v)
    // Clear any empty elements
    if(!$v) unset($request[$k]);
$request = array_values($request);  // Renumber array keys

这将提供一个数字索引数组,表示请求的URI。应用程序可以假定请求中的第一个值是控制器的名称:

if(count($this->request) == 0) 
    $request[] = 'DefaultController';  // Responsible for homepage
$this->controller = new $request[0]( $request );

我还向控制器构造函数传递了一个$context变量,但这超出了本问题的范围(它负责数据库连接、当前用户数据和会话数据)。

之后,它只需调度请求:$this->controller->dispatch()

在调度方法内部,控制器们自己知道请求数组。例如,在您的URL列表中,让我们看一下第三个示例:domain.com/profiles/username:

控制器将被命名为“Profiles”:

class Profiles {
    private $request, $context;
    public function __construct($request, $context) {
        $this->request = $request;
        $this->context = $context;
    }

    public function dispatch() {
        if(count($this->request) == 2 && $this->request[1] == 'username') {
            // Load data from model for the requested username ($this->request[1])

            // Show View
        }
    }
}

有更好的方法将请求向量映射到操作,但希望你能明白要点。


在你的例子中,$this->context = $context;是什么意思? - Dan
为了我的目的,我使用上下文来存储请求的上下文。这并不完全适用于您的问题,因此我没有详细介绍它。它基本上只是一个关联数组,包含对数据库、会话和用户对象的引用。 - Jeff Lambert
@JeffLambert 你好!我在 PHP MVC 路由方面有一个 300 分的悬赏问题。如果您有任何意见,将不胜感激。祝您愉快!https://stackoverflow.com/questions/42172228/is-this-how-an-mvc-router-class-typically-works - Anthony Rutledge

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接