为什么网站的MVC需要一个入口点? [英] Why should MVC for websites require a single point of entry?

查看:75
本文介绍了为什么网站的MVC需要一个入口点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

为什么不仅仅让各个页面作为控制器?我的意思是,如果您的站点上有一个页面列出了所有已注册的成员,则用户导航到的members.php页面将成为成员的控制器.这个php文件将在成员模型中查询数据库中的成员列表,并将其传递到成员视图.

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.

我可能遗漏了一些东西,因为我最近才发现MVC,但是这个问题困扰着我.这样的设计不是更可取,因为不是拥有一个one肿的入口文件,而所有页面都无法直观地调用模型和特定页面的视图,而是从其各自的页面中对其进行了封装,封装和调用?

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?

推荐答案

以我的经验,拥有一个单入口点有两个臭名昭著的优点:

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

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

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.

通过PHP解析URL,可以使虚拟URL"与Web服务器上的物理文件布局脱钩.这意味着您可以轻松更改URL系统(例如,用于SEO或网站国际化),而不必实际更改脚本在服务器中的位置.

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.

但是,有时具有单入点可能浪费服务器资源.显然,这适用于静态内容,但是当您有一组具有非常特定目的的请求并且只需要很少量的资源(例如,它们不需要数据库访问权限)时,这也适用.然后,您应该考虑拥有多个入口点.我已经在我正在处理的网站上完成了此操作.它为所有标准"动态内容提供了一个入口,为公共API的调用提供了一个入口,这需要更少的资源并且拥有完全不同的URL系统.

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.

最后一点:如果站点实施得当,则index.php不必变得肿:)

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

这篇关于为什么网站的MVC需要一个入口点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆