【问题标题】:Most efficient way to route urls in a PHP mvc?在 PHP mvc 中路由 url 的最有效方法?
【发布时间】:2012-06-21 17:20:19
【问题描述】:

我正在开发一个 simple 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();

控制器/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 重写。有人不同意吗?

标签: php model-view-controller oop .htaccess


【解决方案1】:

这是一种类似于您的第二个 .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()

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

控制器将被命名为“配置文件”:

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; 是什么?
  • 出于我的目的,我使用上下文来存储请求的上下文。它不是 100% 适用于您的问题,所以我没有详细介绍它。它基本上只是一个关联数组,包含对数据库、会话和用户对象的引用。
  • @JeffLambert 你好!我有一个关于 PHP MVC 路由的 300 分赏金问题。您可以制作的任何 cmets 将不胜感激。祝你有美好的一天! stackoverflow.com/questions/42172228/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-02-05
  • 1970-01-01
  • 2010-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多