简体   繁体   English

这是如何在PHP OO中使用MVC模式

[英]Is this how to use an MVC pattern in PHP OO

I am trying to build a nice PHP framework for personal use. 我正在尝试为个人使用构建一个不错的PHP框架。 I realize there are many existing but this is a great learning experience that covers a vast majority of different challenges and really teaches me a lot, as well as having a finished product when i'm done that I can hopefully use to develop other projects from and since I am building it, there should be no learning curve on how to use it. 我意识到现有很多,但这是一个很好的学习经验,涵盖了绝大多数不同的挑战,并且真正教会了我很多,并且当我完成了我有希望用于开发其他项目的成品时因为我正在构建它,所以应该没有关于如何使用它的学习曲线。

Some of the basic goals, 一些基本目标,
- Use PHP Object Oriented instead of procedural. - 使用PHP面向对象而不是程序。
- Use an MVC or something similar to learn more about this style. - 使用MVC或类似的东西来了解这种风格的更多信息。
- Be lightweight and fast/good performance - 轻巧,快速/良好的性能

Here is my planned site structure, excluding some other folders for javascript, images, css, some helper functions/files, etc. 这是我计划的网站结构,不包括javascript,图像,CSS,一些辅助函数/文件等的其他文件夹。

///////////// Site structure ///////////// /////////////网站结构/////////////

site.com/
        /index.php
site.com/library/
                /Config.class.php
                /Photos.class.php
                /Mail.class.php
                /Filter.class.php
                /QRcodes.class.php
                /Router.class.php
                /Database.class.php
                /Templates.class.php
                /etc, etc,etc......
site.com/modules/
                /account/
                        /model
                        /views
                        /controllers
                /users/
                        /model
                        /views
                        /controllers
                /messages/
                        /model
                        /views
                        /controllers
                /API/
                        /model
                        /views
                        /controllers
                /forums/
                        /model
                        /views
                        /controllers
                /blogs/
                        /model
                        /views
                        /controllers
               /etc, etc, etc, etc.............
                        /model
                        /views
                        /controllers

I have decided to route all Request through a single point of entry, index.php 我决定通过单个入口index.php路由所有请求
I will build a Router class/object that will match the URI against a map of possible destinations using regular expressions. 我将构建一个Router类/对象,它将使用正则表达式将URI与可能目标的映射进行匹配。 Here is a snippet of what I have for now for this part... 以下是我现在对这部分内容的摘要......

<?php
//get url from URL
$uri = isset($_GET['uri']) ? $_GET['uri'] : null;

$uri_route_map = array( 
    //users/account like http://mysite.com/users/324 (any digit)
    'users/friends/page-(?<page_number>\d+)' => 'modules/users/friends/page-$1',
    'users/friends/edit/page-(?<page_number>\d+)' => 'modules/users/friends/edit/page-$1',
    'users/friends/edit' => 'modules/users/friends/edit',
    'users/friends/' => 'modules/users/friends/',
    'users/online' => 'modules/users/online/' ,
    'users/online/page-(?<page_number>\d+)' => 'modules/users/online/page-$1',
    'users/create' => 'modules/users/create',
    'users/settings' => 'modules/users/settings',
    'users/logout(?<page_number>\d+)' => 'modules/users/logout',
    'users/login' => 'modules/users/login',
    'users/home' => 'modules/users/home',

    //forums
    'forums/' => 'modules/forums/index',
    'forums/viewthread/(?<id_number>\d+)' => 'modules/forums/viewthread/$1',
    'forums/viewforum/(?<id_number>\d+)' => 'modules/forums/viewforum/$1',
    'forums/viewthread/(?<id_number>\d+)/page-(?<page_number>\d+)' => 'modules/forums/viewthread/$1/page-$2',
    'forums/viewforum/(?<id_number>\d+)/page-(?<page_number>\d+)' => 'modules/forums/viewforum/$1/page-$2',

    // TESTING new method to define class and page better!
    'users/home' => array('PAGE CLASS NAME', 'ACTION NAME')

    //blog routes coming soon
    //mail message routes coming soon
    //various other routes coming soon
);

//////////////////////////////////
class Router
{
    public function __construct()
    {
    }

    public function get_route($uri, array $uri_routes)
    {
        foreach ($uri_routes as $rUri => $rRoute) {
            if (preg_match("#^{$rUri}$#Ui", $uri, $uri_digits)) {
                //if page number and ID number in uri then set it locally
                $page_number = (isset($uri_digits['page_number']) ? $uri_digits['page_number'] : null);
                $id_number = (isset($uri_digits['id_number']) ? $uri_digits['id_number'] : null);
                echo '<hr> $page_number = ' . $page_number . '<BR><hr> $id_number = ' . $id_number;
                $uri = preg_replace("#^{$rUri}$#Ui", $rRoute, $uri);
                echo '<BR><BR>Match found: ' . $uri_routes . '<BR><BR>';
                break;
            }
        }
        $uri = explode('/', $uri);
    }
}

$uri = new Router();
$uri = $uri->get_routes($_GET['uri'], $uri_route_map);

?>



PLEASE NOTE 请注意

THE CODE ABOVE IS ALL TEST CODE AND WILL BE CHANGED, IT IS JUST THE CONCEPT 上面的代码是所有测试代码并且将会改变,这只是概念

So as you can see I am planning to have index.php get the URI, check it against valid paths, if one is found, it will include or build a header section, then will build the content section, then finally the footer section of the page. 因此,你可以看到我计划让index.php获取URI,检查它是否有效路径,如果找到一个,它将包含或构建一个标题部分,然后将构建内容部分,然后最后是页脚部分这一页。

If you were to access for example... www.test.com/blogs/userid-32423/page-23 如果您要访问例如... www.test.com/blogs/userid-32423/page-23

The the page would... 页面会......

  • build header() 构建标题()
  • create object blogs... $blogs = new Blogs; 创建对象博客... $ blogs = new Blogs;
  • call $blogs->viewbyID($userID,$paging); 调用$ blogs-> viewbyID($ userID,$ paging); //$userID would be 32423 and $paging would be 23 from the URI // $ userID将是32423,而来自URI的$ paging将是23
  • build footer section 建立页脚部分

Now based on my folder structure. 现在基于我的文件夹结构。 I believe that the blogs class file in our above example would be considered the CONTROLLER . 我相信上面例子中的博客类文件将被视为CONTROLLER If I am correct so far, then this blogs class which is calling blogs->viewbyID(ID,PAGE) the viewbyID method would set up some code, query the database and set up some variables for the page and then it could include a blogs template file. 如果到目前为止我是正确的,那么这个博客类调用blogs-> viewbyID(ID,PAGE)viewbyID方法会设置一些代码,查询数据库并为页面设置一些变量然后它可以包含一个博客模板文件。 This blogs template file could be considered the VIEWS . 此博客模板文件可被视为VIEWS

Now I might have this whole concept wrong, and that is why I posted so much code and text to try and explain my outlook on it, please give me thoughts, suggestions, tell me where im completely wrong, and where I might be on the right track, I will greatly appreciate any constructive critism or thoughts. 现在我可能有这个概念错了,这就是为什么我发布了如此多的代码和文字来尝试解释我对它的看法,请给我一些想法,建议,告诉我哪里我完全错了,我可能在哪里正确的轨道,我将非常感谢任何建设性的批评或思想。 If I am right in my above usage of the View, Controller portion of the MVC pattern, then what part of my code would be considered the Modal? 如果我在上面使用MVC模式的View,Controller部分是正确的,那么我的代码的哪一部分将被视为模态? This is somewhat confusing to me for some reason. 由于某种原因,这对我来说有点混乱。

Bonus question... What about form post, where should I process these at? 奖金问题...表格帖子怎么样,我应该在哪里处理这些? In my example I am focusing on the blog module, so lets say POST for adding new blog entry and POST for editing blog entry, where should these be processed (modal, view, controller)? 在我的例子中,我专注于博客模块,所以让我们说POST用于添加新博客条目和POST用于编辑博客条目,应该在哪里处理(模态,视图,控制器)?

The controller's job is to examine the user's input and determine what is being requested. 控制器的工作是检查用户的输入并确定所请求的内容。 Once that's determined, the model(s) is invoked. 一旦确定,就会调用模型。 The controller then takes the model's payload and gives it to the view. 然后控制器获取模型的有效负载并将其提供给视图。

Basically, the model is a model of the business. 基本上, 该模型是业务的模型 Want to add a blog post? 想要添加博客文章? Then the Blog model will have a ->add or ->save method (which is called by the controller. The blog controller may also have an add method, but it is not for talking to the database. It's for examining the input and then calling the model to do the actual saving). 然后Blog模型将有一个->add->save方法(由控制器调用。博客控制器也可能有一个add方法,但它不是用于与数据库通信。它用于检查输入然后调用模型进行实际保存)。 Model methods don't always interact with a database but they usually do. 模型方法并不总是与数据库交互,但它们通常会这样做。

As far as add/edit, they almost always share the same view and can share the same controller methods if doable. 就添加/编辑而言,它们几乎总是共享相同的视图,并且如果可行则可以共享相同的控制器方法。

Just remember that the controller is the entry point for all your clients. 请记住, 控制器是所有客户的入口点。 Every URL your application handles should map to a controller method. 应用程序处理的每个URL都应映射到控制器方法。 The controller then tells the model what to do, passing the user input to it. 然后控制器告诉模型要做什么,将用户输入传递给它。

Your Models should be the one querying the database, not your Controller. 您的模型应该是查询数据库的模型,而不是您的控制器。 Your Model is there to handle all CRUD actions and, where necessary, pass results back to the Controller to hand to the View. 您的模型可以处理所有CRUD操作,并在必要时将结果传递回Controller以传递给View。 Usual flow would be 通常的流量是

Controller > Model > Controller > View

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM