PHP Template and Mod Rewrite - Limited to using one php file for multiple layout

Hi,

I am using a ModRewrite so that something like “www.site.com/product?productname=iphone-4
can be altered to something like “www.site.com/iphone-4

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.

If a user types:

www.site.com/iphone4-car-charger

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?

Matt.

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.

http://www.example.com/p/iphone-4
http://www.example.com/a/iphone-4-car-charger

or include the product name in the accessories URLs and separate them with a slash

http://www.example.com/iphone-4/iphone-4-car-charger

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.

But too early to recommend to others. 2.0 is still a moving target. I will use Symfony 1.4 any day when in need of a PHP framework, though.

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.

What is Symfony?

I am guessing it has something to do with PHP and Mod Rewrite??

Matt.

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.

In Symfony 2 there are multiple front-end controllers for different environments such as; testing, development and production.