为什么网站的 MVC 需要单点入口?

发布于 2024-10-31 08:40:29 字数 379 浏览 0 评论 0原文

我看到许多网站的 MVC 实现都有一个单入口点,例如 index.php 文件,然后解析 URL 以确定要运行哪个控制器。这对我来说似乎很奇怪,因为它涉及到必须使用 Apache 重写来重写 URL,并且页面足够多,单个文件会变得臃肿。

为什么不直接让各个页面作为控制器呢?我的意思是,如果您的网站上有一个页面列出了所有注册会员,那么用户导航到的 members.php 页面将成为会员的控制器。该 php 文件将从数据库中查询成员模型以获取成员列表,并将其传递到成员视图。

我可能错过了一些东西,因为我最近才发现 MVC,但这个问题一直困扰着我。这种设计不是更可取吗?因为所有页面都不直观地调用特定页面的模型和视图,而不是拥有一个臃肿的入口文件,并且从其各自的页面中包含、封装和调用?

I see many MVC implementations for websites have a single-entry point such as an index.php file and then parses the URL to determine which controller to run. This seems rather odd to me because it involves having to rewrite the URL using Apache rewrites and with enough pages that single file will become bloated.

Why not instead just to have the individual pages be the controllers? What I mean is if you have a page on your site that lists all the registered members then the members.php page users navigate to will be the controller for the members. This php file will query the members model for the list of members from the database and pass it in to the members view.

I might be missing something because I have only recently discovered MVC but this one issue has been bugging me. Wouldn't this kind of design be preferable because instead of having one bloated entry-file that all pages unintuitively call the models and views for a specific page are contained, encapsulated, and called from its respective page?

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

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

发布评论

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

评论(5

奢欲 2024-11-07 08:40:29

根据我的经验,单入口点有几个众所周知的优点:

它简化了集中任务,例如资源加载(连接到数据库或内存缓存服务器、记录执行时间、会话处理等)。如果你想添加或删除一个集中任务,你只需要更改一个文件,即index.php。

在 PHP 中解析 URL 可以使“虚拟 URL”与 Web 服务器上的物理文件布局解耦。这意味着您可以轻松更改 URL 系统(例如,出于 SEO 目的或网站国际化),而无需实际更改脚本在服务器中的位置。

然而,有时拥有单一入口点可能会浪费服务器资源。这显然适用于静态内容,而且当您有一组具有非常特定目的的请求并且只需要很少的资源集(例如,也许它们不需要数据库访问权限)时。那么您应该考虑拥有多个入口点。我已经为我正在开发的网站做到了这一点。它有一个用于所有“标准”动态内容的入口点,还有一个用于调用公共 API 的入口点,后者需要的资源少得多,并且具有完全不同的 URL 系统。

最后一点:如果网站实施良好,您的index.php 不一定会变得臃肿:)

From my experience, having a single-entry point has a couple of notorious advantages:

It eases centralized tasks such as resource loading (connecting to the db or to a memcache server, logging execution times, session handling, etc). If you want to add or remove a centralized task, you just have to change a singe file, which is the index.php.

Parsing the URL in PHP makes the "virtual URL" decoupled from the physical file layout on your webserver. That means that you can easily change your URL system (for example, for SEO purposes, or for site internationalization) without having to actually change the location of your scripts in the server.

However, sometimes having a singe-entry point can be a waste of server resouces. That applies obviously to static content, but also when you have a set of requests that have a very specific purpose and just need a very little set of your resorces (maybe they don't need DB access for instance). Then you should consider having more than one entry point. I have done that for the site I am working on. It has an entry point for all the "standard" dynamic contents and another one for the calls to the public API, which need much less resources and have a completely different URL system.

And a final note: if the site is well-implemented, your index.php doesn't have to become necessarily bloated :)

五里雾 2024-11-07 08:40:29

这都是关于 DRY 的,如果你有很多处理请求的 php 文件,你就会有重复的代码。这只会造成维护噩梦。

看一下 CakePHP 的“主”索引页面,https: //github.com/cakephp/cakephp/blob/master/app/webroot/index.php

无论应用程序有多大,我都不需要修改它。那么怎么会肿呢?

it is all about being DRY, if you have many php files handling requests you will have duplicated code. That just makes for a maintenance nightmare.

Have a look at the 'main' index page for CakePHP, https://github.com/cakephp/cakephp/blob/master/app/webroot/index.php

no matter how big the app gets, i have never needed to modify that. so how can it get bloated?

想你的星星会说话 2024-11-07 08:40:29

当使用 MVC 框架直接深度链接到控制器时,它消除了实现控制器插件或过滤器的可能性,具体取决于您使用的框架。拥有单点入口可以标准化应用程序和模块的引导,并在访问控制器之前执行前面提到的插件。

Zend Framework 也以路由的形式使用它自己的 URL 重写。在我工作的使用 Zend Framework 的应用程序中,有一个 .htaccess 文件,其中可能有 6 行重写规则和条件。

When deeplinking directly into the controllers when using an MVC framework it eliminates the possibility of implementing controller plugins or filters, depending on the framework you are using. Having a single point of entry standardizes the bootstrapping of the application and modules and executing previously mentioned plugins before a controller is accessed.

Also Zend Framework uses its own URL rewriting in the form of Routing. In the applications that use Zend Framework I work on have an .htaccess file of maybe 6 lines of rewriterules and conditions.

扶醉桌前 2024-11-07 08:40:29

单个入口点当然有其优点,但是您可以从每个页面顶部处理数据库连接、会话等的中央必需文件中获得几乎相同的好处。它并不臃肿,它符合 DRY 原则(除了对于那个 require 行),它将逻辑和表示分开,如果您更改文件位置,简单的搜索和替换即可修复它。

我两种都用过,对于我的目的来说,我不能说其中一种明显更好或更差。

A single entry point certainly has its advantages, but you can get pretty much the same benefit from a central required file at the top of every single page that handles database connections, sessions, etc. It's not bloated, it conforms to DRY principles (except for that one require line), it seperates logic and presentation and if you change file locations, a simple search and replace will fix it.

I've used both and I can't say one is drastically better or worse for my purposes.

凉月流沐 2024-11-07 08:40:29

软件工程师正在影响单点入口范例。 “为什么不直接让各个页面成为控制器呢?”

从某种意义上说,各个页面已经是控制器了。

  1. 在 PHP 中,将为每个 HTTP 请求加载一些样板代码:自动加载器 require 语句 (PSR-4)、错误处理程序代码、会话,如果您明智的话,请将代码的核心包装在 try/catch 中将 Throwable 作为要捕获的首要异常。通过集中代码,您只需在一处进行更改!

诚然,集中式 PHP 将至少使用一个 require 语句(来加载自动加载器代码),但即使您有许多 require 语句,它们也将位于一个文件中,index.php(不会分布在一系列文件中)在文档根目录下)。

  1. 如果您在编写代码时考虑到安全性,那么您可能会对每个请求进行某些编码检查/清理/验证。使用 $_SERVERfilter_input_array() 中的值?那么你不妨将其集中化。

底线是这样的。您在每个页面上执行的操作越多,您就越有充分的理由集中该代码。

请注意,这种思维方式会导致人们将网站视为 Web 应用程序。从 Web 应用程序的角度来看,单点入口是合理的,因为解决一次的问题只需要在一个地方进行修改。

Software engineers are influencing the single point of entry paradigm. "Why not instead just to have the individual pages be the controllers?"

Individual pages are already Controllers, in a sense.

  1. In PHP, there is going to be some boilerplate code that loads for every HTTP request: autoloader require statement (PSR-4), error handler code, sessions, and if you are wise, wrapping the core of your code in a try/catch with Throwable as the top exception to catch. By centralizing code, you only need to make changes in one place!

True, the centralized PHP will use at least one require statement (to load the autoloader code), but even if you have many require statements they will all be in one file, the index.php (not spread out over a galaxy of files on under the document root).

  1. If you are writing code with security in mind, again, you may have certain encoding checks/sanitizing/validating that happens with every request. Using values in $_SERVER or filter_input_array()? Then you might as well centralize that.

The bottom line is this. The more you do on every page, the more you have a good reason to centralize that code.

Note, that this way of thinking leads one down the path of looking at your website as a web application. From the web application perspective, a single point of entry is justifiable because a problem solved once should only need to be modified in one place.

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