tags:

views:

59

answers:

2

In most of the tutorials for PHP MVC design structures, a router class is used to take user input and look up the right controller in order to process that input accurately. The input normally takes the form of a url. For example http://example.com/foo/bar/ would ask the router to find the controller named foo and fire the method bar.

PHP also has an auto-include function which requires a consistent naming system to used for your classes. For example, if I wanted to get an instance of the class Foo, I would need the file to be called Foo.php and inside would be a class called Foo.

Unfortunately, in tandem, these mechanisms place competing demands on the naming system. The url is very 'front facing' and as such, clients will often need it to reflect the content of a particular page. For example, if I have a page that gives directions to a venue, the client may need this page to be at http://example.com/venues/directions/. Later the content may change and the number of venues is reduced to 1 and now the client wishes the url to read http://example.com/venue/directions/. Obviously, this is a very trivial example but I think the need to change urls occasionally is clear.

Since the urls are very tightly connected to the controller classes, a change to a url means the class file name, the class name itself and any instances of that class will all require changing. For more complex systems this seems very time consuming, to me.

What sort of solutions are there to this problem? It seems to me that a directory of sorts will be necessary in which relations between urls and controllers are stored such that any url could be used to call any controller regardless of what either is named. Thus we would have a venues controller and a venues url and when the client requests a change in the url, we just make a change in the directory translating the new "venue" into "venues".

I think the best way to do this would be in a database table but this then requires an extra query for every user request. Is a database the right way to implement a directory? Is a directory even the best way to solve the above problem? Is this problem even a problem in the first place???

A: 

A good way to solve this problem is for the MVC framework to allow you to specify your own routing rules. For example, see URI Routing in the CodeIgniter framework's documentation.

To go along with your example, this would allow you to remap requests for /venues/ to /venue.

Justin Ethier
+2  A: 

Some solutions I have seen to this is to explicitly specify a routing "connection" to a controller/action.

For example, in NiceDog, you can specify routes like

R('venues?/directions')
  ->controller('VenueController')
  ->action('directionsAction')
  ->on('GET');

Allowing a regular expression to match the URL. (The expression above will match venue/directions or venues/directions) I like this method, because it decouples the controller naming scheme from the URL naming scheme.

Another more configuration-based approach would be to have some kind of explicit mapping:

$routes = array(
    'venues?' =>
      array('controller'=>'VenueController','action'=>'indexAction'),
    'venues?/directions' =>
      array('controller'=>'VenueController','action'=>'directionsAction')
);

And then a simple dispatch function:

function dispatch($url,$routes) {
    foreach ($routes as $pattern=>$map) {
        if (preg_match($pattern,$url,$matches)) {
            $controller = new $map['controller']();
            $action = $map['action'];
            call_user_func_array(
              array($controller,$action),
              array_slice($matches,1)
            );
            return true;
        }
    }
    throw new Exception('Route not found.');
}
Austin Hyde