Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should MVC for websites require a single point of entry?

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?

like image 299
John Smith Avatar asked Dec 28 '22 22:12

John Smith


2 Answers

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 :)

like image 145
pau.moreno Avatar answered Dec 30 '22 11:12

pau.moreno


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?

like image 39
dogmatic69 Avatar answered Dec 30 '22 10:12

dogmatic69