I am developing my first decent-sized PHP site, and I am a bit unclear about exactly what the "proper wayInch (presuming there ever is really a factor) to deal with clean/friendly/pretty Web addresses within the application.
The way in which I view it, you will find two primary options (I'll make use of a simplified social news site for example):
1. Use mod_rewrite to deal with all potential Web addresses. This could look similar, although not identical, towards the following:
RewriteRule ^article/?([^/]*)/?([^/]*)/?([^/]*) /content/articles.php?articleid=$1&slug=$2 RewriteRule ^customers/?([^/]*)/?([^/]*) /content/customers.php?userid=$1&username=$2 RewriteRule ^search/?([^/]*)/? /content/search.php?query=$1
2. Pass everything with a handler script and allow it to be worried about the particulars:
RewriteCond % !-f RewriteCond % !-d RewriteRule (.*) handler.php?content=$1
Clearly this really is all untested "air code," however, you understand.
- Is just one of both of these ways likely to be seriously reduced compared to other? Most probably the mod_rewrite is reduced, since I'm going to be instructed to use .htaccess files with this.
- Exist serious disadvantages either to of those approaches?
- It is possible to "best practice" with this kind of factor, or perhaps is it something which each developer has a tendency to decide upon themselves? I understand WordPress uses option two (although it was more trouble of computer was worth after i looked into just how they made it happen).
Option 1 (.htaccess and many .php files) was frequently used "previously" now, option 2 (every request dealing with one .php file) used much more.
The primary advantages with option 2 are :
- you can include / modify any type of URL without needing to change any physical file like .htaccess
- meaning the format from the Web addresses could be set up within the admin portion of the application, for instance
- you simply have one access point for your PHP code.
- meaning everything goes though
index.php: if you want some code performed for those demands, place it there, and you are sure it'll continually be performed.
- That's used a great deal with MVC frameworks, for example.
- meaning everything goes though
A few years ago, I'd go with option 1 since I personally use MVC and Frameworks, I usually opt for option 2.
Really this is actually the "are frameworks worth using?" question in disguise.
Using mod_rewrite to define your URL routes is fast and simple (should you understand regular expressions...) however your application code is oblivious towards the Web addresses unless of course you duplicate the data somewhere.
Usually, people duplicate these details many occasions without considering it, by hard-coding Web addresses within the links within their sights, or perhaps in redirects. This is actually untidy, and can eventually cause discomfort when you choose to alter the URL structure of the site midway through development. You are certain to miss one and finish track of a 404 somewhere.
Utilizing a routing component inside your application (like the one out of Symfony) means you are able to attach names for your routes, permitting you to definitely define your Web addresses once and re-rely on them many occasions:
# applications/frontend/config/routing.yml home page: url: / param:
This causes it to be quite simple to connect to pages of the site without repeating yourself:
<?php echo url_for('@homepage') ?>