1
\$\begingroup\$

I am learning MVC, and trying to understand routing.

I understand that if I want to show an individual blog post for site.com/54, my route would be something like

//index.php
$controller = new Controller();
if ( preg_match("#/(0-9.)#", $uri, $matches) ) {
 $controller->showAction($matches[1]);
}

which would lead to my controller

//controllers.php
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

//index.php
...
} elseif ($uri == '/about') {
 $controller->aboutAction();
} elseif ($uri == '/contact') {
 $controller->contactAction();
} elseif ($uri == '/') {
 $controller->indexAction();
} else {
 $controller->missingAction();
}

My question is, what about the in-between? For example, apple.com/ipad/overview/ and apple.com/ipad/features/. 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?

Malachi
29k11 gold badges86 silver badges188 bronze badges
asked Jan 31, 2013 at 19:20
\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Aug 19, 2015 at 21:19

1 Answer 1

2
\$\begingroup\$

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) ) {
 $controller->showOverview($matches[1]);
}

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

/:product
/:product/specs
/:product/comments
/about

You should check out the Slim Framework: http://www.slimframework.com/

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

answered Feb 1, 2013 at 15:10
\$\endgroup\$
2
  • \$\begingroup\$ +1 (not enough rep), this makes sense to me. Btw, what does :product mean? I've seen this markup before. Why not $product? \$\endgroup\$ Commented Feb 1, 2013 at 15:23
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ Commented Feb 1, 2013 at 20:01

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.