适用于MVC路由功能的解决方案解析请求的视图

适用于MVC路由功能的解决方案解析请求的视图

本文介绍了PHP:适用于MVC路由功能的解决方案解析请求的视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现MVC设计结构目前正在努力解决请求的意见。

I want to implement the MVC design structure and currently struggeling with an good solution to parse requested views.

在我的路由文件中,我有以下代码:

In my routing file, I have following code:

public function parseRequestedView() {

   $this->ressource_requested = explode('/', trim($_GET['view'], '/'));

   // e.g.: http://www.foo.com/article/{id}/comments/show
   if (!empty($this->ressource_requested[3])) {

      // Format: [0] viewpoint (article), [1] child (comments), [2] action (show), [3] reference ({id}),
      //         [4] additional information (from $_POST)
      return array($this->ressource_requested[0], $this->ressource_requested[2], $this->ressource_requested[3],
                   $this->ressource_requested[1], $_POST);

   // e.g.: http://www.foo.com/article/{id}/show
   } elseif (!empty($this->ressource_requested[2])) {

      return array($this->ressource_requested[0], NULL, $this->ressource_requested[2], $this->ressource_requested[1],
                   $_POST);

   // e.g.: http://www.foo.com/archive/show
   } else {

      return array($this->ressource_requested[0], NULL, $this->ressource_requested[1], NULL, NULL);

   }

}

,无论访问者在浏览器中输入什么内容,函数将解析请求,并始终返回相同格式的数组/输出。主机名以后的URL的第一个段始终是主要观点(例如:文章)。最后,我通过另一个函数includeTemplateFile()来包含视图。这些文件具有这个命名约定:

The idea is, no matter what a visitor types into the browser, the function parses the request and always returns the same formatted array/output. The first segment of the URL following the hostname is always the main viewpoint (e.g.: article). In the end, I am including the view through another function called includeTemplateFile(). The files have this naming convention:

viewpoint.child.action.template.php
e.g.: article.comments.show.template.php

我的问题现在是:有更优雅的解决方案吗?我读了一些图章/文章(例如:),但是我并不喜欢大多数解决方案,因为它们的设计不好。

My question is now: Is there a more elegant solution? I read some of the turorials/articles (e.g.: http://johnsquibb.com/tutorials/mvc-framework-in-1-hour-part-one) regarding this topic, but I do not like most solutions since they are not well designed.

以下是.htaccess文件的内容:

Here is the content of the .htaccess file:

RewriteEngine on

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

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

提前感谢

推荐答案

好的,我刚开始说我不是PHP的专家,我建议使用一个框架,如交响乐来做你的路由,但这里是一个可能的解决方案,你可以使用。

Ok I'm just gonna start by saying I'm no expert in PHP and I recommend using a framework such as symphony to do your routing, but here is one possible solution that you could use.

function regexPath($path)
{
    return '#' . str_replace([":int:", ":string:"], ["\d+", ".+"], $path) . '#';
}

function parseRequestedView($url)
{
    $ressource_requested = explode('/', trim($url, '/'));


    // define our routes, and the indices that each route will use (from the exploded url)
    // this could be defined as another parameter or as a member of the class
    $routes = [
        regexPath("article/:int:/comments/show") => [0,  2, 3,  1], // will return array(resource[0], resource[2], resource[3], resource[1]), etc
        regexPath("article/:int:/show")          => [0, -1, 2,  1], // -1 will return a null
        regexPath("archive/show")                => [0, -1, 1, -1]
    ];


    // go through each route, checking to see if we have a match
    foreach ($routes as $regex => $indices)
    {
        if (preg_match($regex, $url))
        {
            // it matched, so go over the index's provided and put that data into our route array to be returned
            foreach ($indices as $index)
            {
                $route[] = $index > -1 ? $ressource_requested[$index] : null;
            }

            // include the post data (not really nessesary)
            $route[] = $_POST; // unnessesary to pass $_POST data through function, because it is global

            return $route;
        }
    }

    return null; // or some default route maybe?
}

$route = parseRequestedView("article/13/comments/show");

echo '<pre>';
print_r($route);
echo '</pre>';

/* returns:
Array
(
    [0] => article
    [1] => comments
    [2] => show
    [3] => 13
    [4] => Array // this is our $_POST data
        (
        )

)
*/

这篇关于PHP:适用于MVC路由功能的解决方案解析请求的视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 16:20