Comment 1 for bug 121560

Revision history for this message
Sander van Schouwenburg (Sandworm) (sander-sinaasappel) wrote :

Attached are ModuleDispatcher and BaseDispatcher which are extensions to MultiURLDispatcher and URLDispatcher respectively.

The generate_url() functions probably work as expected. $mymultiurldispatcher->generate_url('mymodule', 'mycommand', array('id' => 5)) returns $mydispatcher->generate_url('mycommand, array('id' => 5)) prefixed with the prefix for 'mymodule', where $mydispatcher is an instance of the class which is mapped to 'mymodule'.
$mydispatcher->generate_url('mycommand, array('id' => 5)) returns an url part for the first mapping with command 'mycommand' and if it contains and ':id' part it will be substituted by 5. It returns a trailing slash depending on the 'force-trailing-slash' option.

I expect that this code can quite safely be inserted in the original MultiURLDispatcher and URLDispatcher classes. Only note that on line 50 the line
  $dispatcher = BaseDispatcher::get_instance($class_name);
must be replaced with:
  $dispatcher = URLDispatcher::get_instance($class_name);

KNOWN BUGS:
- When you want to generate the url of a certain module, the dispatcher for that module must be loaded.
- When you have multiple mappings to the same command name, the first one found will be used, regardless of possible different arguments.
- The arguments to the urls are not checked for existance.
  i.e.: /item/:id/ will be resolved to '/item/'.$params['id'].'/' regardless of whether $params['id'] actually is set.