php - 在 PHP mvc 中路由 url 的最有效方法?

标签 php model-view-controller oop .htaccess

我正在开发一个简单 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 中获取要使用的 Controller 和方法,使用:

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

但是使用这种方法我不明白您如何仅使用数据调用 Controller 内的操作?

难道你不需要做这样的事情吗:

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

结论

我只是不明白如何最好地为一个有很多不同格式的 seo 友好 url 的网站做路由,我明白 mvc 应该如何工作,但我正在努力掌握路由部分和所有我遇到的例子似乎非常复杂!

我真的很想知道如何编写 .htaccess 和 Controller 来处理大量不同格式的 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 示例的方法。

$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 的数字索引数组。应用程序可以假定请求中的第一个值是 Controller 的名称:

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

我还将 $context 变量传递给 Controller ​​构造函数,但这超出了这个问题的范围(它负责数据库连接、当前用户数据和 session 数据)。

之后,它简单地分派(dispatch)请求:$this->controller->dispatch()

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

Controller 将被命名为“配置文件”:

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
        }
    }
}

有更好的方法可以将请求向量映射到操作,但希望您能掌握要点。

关于php - 在 PHP mvc 中路由 url 的最有效方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11104865/

相关文章:

php - Eloquent 模型添加没有 "created_at"和 "updated_at"字段的行

c# - 打破继承链

c++ - 从函数返回对对象的静态常量引用

php - 在 PHP 中按自定义顺序重新排列对象数组

model-view-controller - 模型- View - Controller 是开发 Web 应用程序的最佳设计模式吗?

php - 根据要求从 50k 行表中选择随机行。太慢了。我怎样才能加快速度?

model-view-controller - 与Web应用程序中的三层体系结构相比,MVC有何优势?

java - 从日历类java获取生日

php - 使用 <button> 增加库存

javascript - 移动时给输入一个值,然后通过ajax保存它 - php/jquery -php