Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am learning MVC, and trying to understand routing.

I understand that if I want to show an individual blog post for, my route would be something like

$controller = new Controller();
if ( preg_match("#/(0-9.)#", $uri, $matches) ) {

which would lead to my controller

class Controller
    private function showAction($id) {
        $post = getPostById($id);
        $view = new View('views/show.php');

And, on the other hand, a sensible thing to do for different static pages is perhaps

} elseif ($uri == '/about') {
} elseif ($uri == '/contact') {
} elseif ($uri == '/') {
} else {

My question is, what about the in-between? For example, and They are different pages, so it seems like the latter is sensible (i.e. unique controller method for each page). But then, when you're on /overview, you still need to know you're a sub-page of /ipad (so you can highlight the iPad button in the navigation). If you did the first method (regex parsing), you'd know that (i.e. you could use $matches[1] when constructing your navigation). If you use the second method, it seems like in your view you'd have to do something like $current_product = 'ipad' so your nav would know about it. But this could get cumbersome.

What's the appropriate method?

share|improve this question
The desire to improve code is implied for all questions on this site. Question titles should reflect the purpose of the code, not how you wish to have it reworked. See How to Ask. – Jamal Aug 19 '15 at 21:19
up vote 2 down vote accepted

Have you thought about calling the overview only when the URI ends in "overview" and preceded by a product slug?

$controller = new Controller();
if ( preg_match("#^/([a-zA-Z0-9_-]+)/overview/?$#", $uri, $matches) ) {

This way, you define your routes with the following assumption:


You should check out the Slim Framework:

I've used it just for routing when performance was a crucial requirement.

share|improve this answer
+1 (not enough rep), this makes sense to me. Btw, what does :product mean? I've seen this markup before. Why not $product? – Sam Selikoff Feb 1 '13 at 15:23
Sam, the notation :key is used by many routing frameworks to denote a named parameter in the URL. Instead of using the regular expression (?<product>[a-zA-Z]+), a framework like Slim will allow you to say :product and pass that value to your controller's action. – Dimitry Feb 1 '13 at 20:01

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.