br3nt
br3nt

Reputation: 9586

Is it better to have a php front loader or use .htaccess to load controllers?

I came across this article, How to implement a front controller. The article suggests that a better way to load controllers is to leave it to apache as this is what it was designed for.

So I have a few questions...

  1. Is using .htaccess a viable alternative to using php for routing requests to controllers?
  2. Which way is better, faster, modular and portable?
  3. Has anyone actually implemented an mvc framework in this way? If so, got any tips?
  4. Does anyone know of any websites that discuss this technique (I couldn't find anything in
    google)?

Upvotes: 2

Views: 1940

Answers (2)

drew010
drew010

Reputation: 69967

Maybe its the beer, but that article made little sense to me, it also put a lot of "words" in quotes. I disagree with some things mentioned in there. It does say ...that this approach to implementing a Front Controller in PHP does alot (sic) to raise the learning curve required to become fluent with the framework. Sure I suppose thats true, but when has any powerful, flexible, and large system not required a little bit of learning.

In regards to your questions:

  1. .htaccess could be a somewhat viable alternative to using PHP, but is much less extensible and gets complicated and hard to manage quickly. You can do URL configurations like this in Apache, lighttpd, nginx, and I've seen it done on occasion, but to some doing it this way would be a big learning curve.

    If you use PHP to do the routing, it can get its route information from config files, arrays, or even injected via an object. This gives you much flexibility and makes it possible to include or exclude routes depending on many factors.

  2. Using the server config file to configure URL routing might be somewhat faster, but the difference is minute. Server config would be far less modular, and not portable across different HTTP servers. The native language front controller works on any server platform.

  3. I've not seen an MVC framework that does that, but I haven't investigated many outside of PHP.

  4. Can't help on this one either.

Personally, in PHP I use Zend Framework a lot. It uses a front controller pattern that routes everything through one script. I've not had any limitations in this method, it provides everything and more than I need.

Those are my thoughts, hope it helps.

Upvotes: 2

Hamish
Hamish

Reputation: 23346

The primary objection in that article to using a single entry point seems to be:

...what about when you have hundreds of Page Controllers? You end up with a massive switch statment or perhaps something disguised in an array, an XML document or whatever. For every page request, PHP will have to reload a bunch of data which is irrelevant to the current request the user is trying to perform.

That's a very weak argument. Firstly, that's a terrible way to implement a routing mechanism. Secondly, an application would have to be considerably complex for this to have any measurable effect - and if an application is this complex, it's likely that any performance hit at the entry point is minimal compared to the execution of the rest of the application.

And consider: if a PHP script for handling the front end of a complex web app is hard to maintain, imagine what the equivalent .htaccess file would look like!

Finally, you can avoid the issue with a bytecode cache, making the "problem" of loading the script for every request moot.

Upvotes: 3

Related Questions