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 attempting to refactor my app using the MVC paradigm.

My site displays charts. The URLs are of the form


I am using Apache Rewrite to route all requests to index.php, and so am doing my URL parsing in PHP.

I am working on the enduring task of adding an active class to my navigation links when a certain page is selected. Specifically, I have both category-level navigation, and chart-level sub-navigation. My question is, what is the best way to do this while staying in the spirit of MVC?

Before my refactoring, since the nav was getting relatively complicated, I decided to put it into an array:

$nav = array(
  '25th_monitoring' => array(
    'title'    => '25th Monitoring',
    'charts' => array(
      'month_over_month' => array(
        'default' => 'month_over_month?who=total&deal=loan&prev='.date('MY', strtotime('-1 month')).'&cur='.date('MY'),
        'title'   => 'Month over Month'),
      'cdu_tracker' => array(
        'default' => 'cdu_tracker',
        'title'   => 'CDU Tracker')
  'internet_connectivity' => array(
    'title'   => 'Internet Connectivity',
    'default' => 'calc_end_to_end',
    'charts' => array(
      'calc_end_to_end' => array(
        'default' => 'calc_end_to_end',
        'title' => 'calc End to End'),
      'quickcontent_requests' => array(
        'default' => 'quickcontent_requests',
        'title' => 'Quickcontent Requests')

Again, I need to know both the current category and current chart being accessed. My main nav was

    <?php foreach ($nav as $category => $category_details): ?>
    <li class='<?php $current_category == $category ? null : 'active'; ?>'>
      <a href="<?php echo 'http://' . $_SERVER['SERVER_NAME'] . '/' . $category . '/' . reset(reset($category_details['monitors'])); ?>"><?php echo $category_details['title']; ?></a>
    <?php endforeach; ?>

and the sub-nav was something similar, checking for current_chart instead of current_category.

Before, during parsing, I was exploding $_SERVER['REQUEST_URI'] by /, and breaking the pieces up into $current_category and $current_monitor. I was doing this in index.php. Now, I feel this is not in the spirit of the font controller. From references like Symfony 2's docs, it seems like each route should have a controller. But then, I find myself having to define the current category & monitor multiple times, and either within the template files themselves (which doesn't seem to be in the spirit of MVC), or in an arbitrary function in the model.

What is the best practice here?

Update: Here's what my front controller looks like:

// index.php
// Load libraries
require_once 'model.php';
require_once 'controllers.php';

// Route the request
$uri = str_replace('?'.$_SERVER['QUERY_STRING'], '', $_SERVER['REQUEST_URI']);
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && (!empty($_GET)) && $_GET['action'] == 'get_data') {

  $function = $_GET['chart'] . "_data";
  $dataJSON = call_user_func($function);
  header('Content-type: application/json');
  echo $dataJSON;

} elseif ( $uri == '/' ) {

} elseif ( $uri == '/25th_monitoring/month_over_month' ) {

} elseif ( $uri == '/25th_monitoring/cdu_tracker' ) {

} elseif ( $uri == '/internet_connectivity/intexcalc_end_to_end' ) {

} elseif ( $uri == '/internet_connectivity/quickcontent_requests' ) {

} else {
  header('Status: 404 Not Found');
  echo '<html><body><h1>Page Not Found</h1></body></html>';   


It seems like when month_over_month_action() is called, for instance, since the controller knows the current_chart is month_over_month, it should just pass that along. This is where I'm getting tripped up.

share|improve this question
it is ideal situation when you need 1-to-1 route-to-controller. Real life case can be more complicated and I would use one controller to handle this identical operations – Serg Jan 30 '13 at 12:41
You reference Symfony2 documentation. Are you using Symfony2? – Rob Apodaca Jan 30 '13 at 23:17
I started, but then decided I should step back and really understand OOP first. I'm trying to figure out the routing stuff first, since I'm going back and refactoring my projects, and that seems like me to be the first step. – Sam Selikoff Jan 31 '13 at 3:05

You could try urls like this:


Where your application controller structure looks like this:


You could think of "Category" as a module (or in Symfony, a bundle).

The route pattern would be: /category/{category}/chart/{chart} and ChartController might look like this:

class ChartController
    showAction($category, $chart)
        //look up the category and chart to display

Have a look at the symfony2 routing documentation. Also, symfony2's routing is a stand alone routing component that you could use in your index.php/front controller.

share|improve this answer
I have built something else that looks like this, but I'd like to have some more flexibility in my urls. Ultimately, I just want to know the right way to do it, in a way that is robust. – Sam Selikoff Jan 31 '13 at 18:07

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.