But if I have a separate template for products and accessories (say I use template products.php and accessories.php) with different layouts the Mod Rewrite becomes impossible.
The Mod Rewrite will not know whether to use products.php or accessories.php!
What I intend to do is:
Have a field in the MySQL database called productoraccessory and it will indicate whether it is a product or accessory and then enable the right coding to be selected. This will make the Mod Rewrite possible (by checking the productoraccessory for the productid passed in the URL).
$data = mysql_query("SELECT * FROM products WHERE productid = '".$_GET['product']."'");
if($productoraccessory = accessory) {
include("accessories.php");
}
else{
include("products.php");
}
Is this the only way to do a Mod Rewrite with simple URL’s?
That’s right, when there is no way to map the URLs to specific resources using only regular expressions, you have to implement the mapping in your application.
Consider something like WordPress where any URL can be a post, a page, a category archive, a date archive… everything gets rewritten to index.php and index.php decides which code to run and which template to use.
It’s called a front controller.
If you want to do the routing at the web server, you only need a single character to differentiate your two patterns.
Yes, mapping URLs to controllers at the application layer offers much more flexibility. You are not limited to string matching but request type and any other requirements needed to differentiate similar routes. Once you have a routing system set up at the application layer the htaccess file only needs one or two rewrites to hide the front-end controller. I just started looking a Symfony 2 and I must say I think it has one of the easiest to understand and hihgly flexible routing interface available.
True, that said the Symfony 2 routing docs offer great insight into common patterns when dealing with routing at the application level. Its like the creators looked at how everyone else does it figured out what sucked and what worked then created theirs omitting everything that sucks about how other frameworks handle it. Not to mention the docs are spot on and easy to understand. Not only teaching about how to use the framework but providing insight into common patterns.
I have never used Symfony up to this point. I must say just after two days of reading the docs (on the model docs right now) Symfony 2 blows away every other framework. For now on when people ask about MVC and all that other gaze I think I’m just going to refer them to the Symfony 2 docs because it covers just about everything and is actually easy to understand.
An example of a framework (a set of classes designed to work together to help you write software) which uses a front controller (all requests get rewritten to its index.php).
There are separate controllers in all versions of Symfony, but this isn’t a thread about Symfony, this information is at best irrelevant and at worst confusing the lesson here.