1

可能重复:
MVC 中的 CMS 路由

我想实现 MVC 设计结构,目前正在努力寻找解析请求视图的好解决方案。

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

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() 的函数来包含视图。这些文件具有以下命名约定:

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

我现在的问题是:有更优雅的解决方案吗?我阅读了一些关于这个主题的教程/文章(例如:http://johnsquibb.com/tutorials/mvc-framework-in-1-hour-part-one ,但我不喜欢大多数解决方案,因为它们不是精心设计。

这是 .htaccess 文件的内容:

RewriteEngine on

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

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

提前致谢。

4

1 回答 1

0

好的,我首先要说我不是 PHP 专家,我建议使用诸如 symphony 之类的框架来进行路由,但这里有一个您可以使用的可能解决方案。

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
        (
        )

)
*/
于 2012-12-18T02:01:26.693 回答