MVC:嵌套视图和控制器(对于网站)

发布于 2024-09-02 03:01:39 字数 441 浏览 4 评论 0原文

我即将使用 MVC 模式制作一个 PHP 网站。我没有使用框架,因为该网站相当简单,我觉得这将为我提供一个直接了解该模式的好机会。我有几个问题。

问题一:我该如何整理自己的观点?我正在考虑有一个页面视图,其中包含页眉和页脚,并且允许内容视图嵌套在它们之间。

问题 2:如果我有 5 个内容页面,我是否应该创建 5 个不同的视图来用作嵌套在页面视图中的内容?或者,我应该让它们全部扩展一个名为 AbstractContent 的抽象视图吗?

问题3:控制器怎么样?我认为至少应该有一个主控制器。但是请求从那里去哪里呢?到另一个控制器?或者我应该只调用页面视图并保留它?我认为控制器应该处理输入,可能修改模型,并选择视图。但是,如果控制器调用的视图中嵌套的视图之一需要解析额外的输入,该怎么办?

问题4:控制器是否允许向视图传递参数?或者控制器应该简单地修改模型,从而影响视图?或者该模型仅用于数据库访问等?

I'm about to do a PHP website using the MVC pattern. I am not using a framework as the site is fairly simple and I feel that this will give me a good opportunity to learn about the pattern directly. I have a couple questions.

Question 1: How should I organize my views? I'm thinking of having a Page view which will have the header and footer, and which will allow for a Content view to be nested between them.

Question 2: If I have 5 Content pages, should I make 5 different views that can be used as the content that is nested within the Page view? Or, should I make them all extend an abstract view called AbstractContent?

Question 3: What about controllers? I think there should be one main controller at least. But then where does the request go from there? To another controller? Or should I just call the Page view and leave it at that? I thought that controllers were supposed to handle input, possibly modify a model, and select a view. But what if one of the views nested within the view that a controller calls requires additional input to be parsed?

Question 4: Are controllers allowed to pass parameters into the view? Or should the controller simply modify the model, which will then affect the view? Or is the model only for DB access and other such things?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

莫相离 2024-09-09 03:01:39

1 - 这是一个偏好问题。最简单的方法是拥有单独的页眉和页脚文件。然后你可以在你的页面控制器中做类似的事情

$title="Page Title";
$var1 = 'var1';
$var2 = 'var2';
$var3 = array("asdf","adsfasdf","234");    

include(HEADER); //$title is in header
include(DIR_VIEWS . 'page.php'); //$var1/2/3 are in page.php
include(FOOTER);

// variable were created before pages were included so they will be set in the templates

如果你要走嵌套路线,你将不得不开始摆弄 str_replace 并开始走向模板引擎,超出了这个答案的范围。

2 - 无需创建视图对象。 “视图”可以只是文件系统上的一个文件,其中包含该视图的 html。就像我上面的例子。这些页面也可以包含基本的 php 循环/回显变量。

3 - 您正在描述前端控制器(有时称为调度程序或路由器)。这确实是可行的方法。有几种创建前端控制器的方法。

您可以拥有指向控制器的 url 数组。

$routes = array (

    '~^/home/$~' => 'home.php',
    '~^/contact/$~' => 'contact.php',
    '~^/blog/.*?$~' => 'blog.php'

);

或者您可以使用 url 中的第一个“目录”作为控制器名称并从控制器目录加载该文件。

4 - 控制器的全部目的是从模型获取信息并将数据传递到视图。


Edited for comment


如果您希望一堆视图有一个侧边栏,您只需将该视图包含在另一个视图中即可。例如:

<div id="content">
    <p>lorem ispum stuff</p>
</div>
<?php include(DIR_VIEWS . 'sidebar.php');

只需确保在“控制”带有侧边栏的页面的控制器中包含一些侧边栏功能的代码:

if ( $_GET['keywords'] ) {
    $sidebar_search_results = get_search_results($_GET['keywords']);
}
// this code should be in a file that you include

$sidebar_search_results 可以是侧边栏视图解析和显示的结果数组。

1 - This is a matter of preference. The simplest way would be to have a separate header and footer file. Then you could do something like this in your page controller

$title="Page Title";
$var1 = 'var1';
$var2 = 'var2';
$var3 = array("asdf","adsfasdf","234");    

include(HEADER); //$title is in header
include(DIR_VIEWS . 'page.php'); //$var1/2/3 are in page.php
include(FOOTER);

// variable were created before pages were included so they will be set in the templates

If you were to go the nested route you would have to start fiddling with str_replace and that starts heading towards a template engine, out of scope for this answer.

2 - No need to make views objects. A "view" can just be a file on your filesytem that contains the html for that view. Like my example above. These pages can contain basic php to loop/echo variables as well.

3 - You are describing a front controller (sometimes called dispatcher or router). This is really the way to go. There are a couple methods for creating a front controller.

You can have an array of urls that point to controllers.

$routes = array (

    '~^/home/$~' => 'home.php',
    '~^/contact/$~' => 'contact.php',
    '~^/blog/.*?$~' => 'blog.php'

);

or you can use the first "directory" in the url as the controller name and load that file form your controller directory.

4 - The entire point of the controller is to get info from the model and pass the data to the view.


Edited for comment


If you want a bunch of views to have a sidebar you just include that view in the other view. For example:

<div id="content">
    <p>lorem ispum stuff</p>
</div>
<?php include(DIR_VIEWS . 'sidebar.php');

Just make sure that in controllers that "control" pages with sidebars you include some code for sidebar functions:

if ( $_GET['keywords'] ) {
    $sidebar_search_results = get_search_results($_GET['keywords']);
}
// this code should be in a file that you include

$sidebar_search_results could be an array of results that your sidebar view parses and displays.

执手闯天涯 2024-09-09 03:01:39
  1. 考虑一下您希望 HTTP 响应是什么样子:带/不带导航的整页、用于打印的剥离页面、JSON 和 HTML 格式。 XML 响应、索引/站点地图。当您感觉网站正在形成后,添加越来越多的快捷方式,以便用尽可能少的代码获得您的响应。
  2. 如果页面布局相似,我将使用相同的视图并从模型(可能是数据库)将内容加载到其中。
  3. 查看前端控制器模式:您应该始终能够在一个点上交叉请求的条目。我会按层次结构将一些内容放在控制器前面,然后每个“主页”(论坛、博客、新闻)有一个控制器。这足以控制,但您必须决定哪些块对您来说足够大/小。
  4. 控制器负责传递到视图中的所有内容。控制器应该获取数据和设置和不是从模型中传递到视图。
  1. Think about what ways you'd want your HTTP responses to look like: full pages with/without nav, stripped pages for printing, JSON & XML responses, an index/sitemap. After you feel the site is forming, add more and more shortcuts for getting your response out there with as little code as possible.
  2. If the page layout is similar, I would use the same view and load content into it from a model (possibly a database).
  3. Check out the Front Controller pattern: you should always be able to intersect the request in a single point of entry. I would put something hierarchally in front of your controllers and then have one controller per "main page" (forum, blog, news). This is sufficient to control, but you'd have to decide what chunks are large/small enough for you.
  4. Controllers are responsible for everything that gets passed into the views. Controllers should fetch data & settings & what-not from models and pass on to the views.
暗恋未遂 2024-09-09 03:01:39

问题1:

这确实是一种方法,也是我一直使用的方法。

问题 2:

让视图尽可能简单。我倾向于只创建 5 个单独的视图(纯 php 文件)。

问题3:

在正常的mvc模式中,有一个前端控制器(操作系统只是一个引导文件,index.php)执行一个控制器。

在 HMVC 中,控制器可以向其他控制器发送附加请求。

问题 4:

普通的 MVC 模式适用于普通的应用程序,其中视图是持久的,并且可以观察模型。对于 Web 应用程序,这是不可能的,因为每个请求都会重新加载所有内容。所以最常用的模式是让控制器将参数传递给视图。

Question 1:

This is indeed a way to do this, and one which I allways use.

Question 2:

Just keep views as simple as possible. I tend to create just 5 separate views (plain php files).

Question 3:

In the normal mvc pattern, there is one front controller (which OS just a bootstrap file, the index.php) which executes one controller.

In HMVC, controllers can send additional request to other controllers.

Question 4:

The normal MVC pattern applies on normal apps, where views are persistent, and can observe the models. With web applications this is not possible, because every request everything is reloaded. So the most used pattern is to let the controller pass the parameters to the view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文