简体   繁体   中英

A web application from one routing script: what are the pros and cons?

As a PHP programmer (just for your context), I generally create a new file for each page in the application. Of course, one script might handle a small collection of views (such as a particular sub-module of the system).

However, with the likes of node.js and Slim (PHP framework) I've seen that they have one routing script.

In that core routing script, one defines how the application responds to requests for different parts of the application.

However, I'm used to doing the same thing with ReWrite rules in .htaccess, which again points to multiple scripts throughout my app.

What are the pros and cons with defining the core of your web application in one single script?

Rewrite rules are not portable. They require apache and mod_rewrite. Using php-based routing however just needs a webserver where you can ensure all requests go through that script.

As far as I know, there are only pros:

  • Keep only a single file in the document root (other than js, css, and media)
  • You don't have to always remember to add include 'all-pages-setup.php' or what have you to every page. You only have to do it once (if you need it at this point).
  • URLs become prettier more or less automatically.

Pros:

  • clarity: you always know where to start when tracing execution
  • reduces duplicated code used by all requests
  • makes it easy to add code later which you want to be executed all requests, say for profiling
  • decouples the request URL from the implementation .php file path, making it simpler to support friendly URLs

But the main one for me is this:

  • no debugging bleedin' 500 internal server errors when you stuff up the .htaccess

Incidentally the technique is known as the Front Controller Design Pattern .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM