PHP:MVC的路由功能来解析请求的意见很好的解决? [重复](PHP: Good soluti

2019-10-17 12:16发布

可能重复:
CMS路由在MVC

我想实现MVC设计结构,目前有很好的解决方案来解析请求的意见struggeling。

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

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

我的问题是现在:有没有更好的解决方案? 我看了一些turorials /文章:(例如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]

提前致谢。

Answer 1:

好吧,我说我在PHP中没有专家,我建议使用一个框架,如交响乐做你的路由我只是要开始,但这里是一个可能的解决方案,你可以使用。

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: Good solution for MVC routing function to parse requested views? [duplicate]