What I’m currently doing is this:
I have a $path variable, which is everything after index.php/ (which I hide with .htaccess) up to a question mark to ignore the querystring.
Then I use a switch with preg_match cases on that variable to determine what script it should call. For example:
switch (true)
{
case preg_match('{products/view/(?P<id>\d+)/?}', $path, $params):
require 'view_product.php';
break;
...
default:
require '404.php';
break;
}
This way I can access the product id just using $params['id'] and, if needed, use the querystring for filtering, pagination, etc.
Is there anything wrong with this approach?
You shouldn’t use
switchlike this.Better use an array and
foreachlike: