No, hook_menu_alter()
is not thought to dynamically add new routes. As any similar alter hook, it is thought to alter the routes defined from other modules.
It would not work for dynamically adding new routes for the simply fact that, similarly to hook_menu()
is only invoked in specific cases, for example, when a module is installed, uninstalled, enabled, or disabled.
If you need to have dynamic pages, you need to do what Drupal does for nodes: You define a route with wildcards, which is similar to the node/%node route defined for nodes.
hook_menu()
has some example for this kind of routes, under Wildcards in Paths.
For example, a route definition as the following could be used to serve a page like my-module/1, my-module-2, etc. mymodule_abc()
would recive the wildcard value as argument, and it could use that argument to access data from a database table, which is essentially what Drupal does when a user visits node/1: It loads the node whose ID is equal to 1 from the node table, and it shows it to the user who requested that page.
$items['my-module/%'] = array(
'page callback' => 'mymodule_abc',
'page arguments' => array(1),
);
As side note, as said in Auto-Loader Wildcards, instead of my-module/%, you could use my-module/%mymodule_abc, and mymodule_abc_load()
will be called every time a page like my-module/1 is accessed, getting the wildcard value (1) as argument; it could then return the object saved in the database from the form submission handler.
See node_load()
as example. That function returns the object saved in the database from the form submission handler of the node edit form.
Auto-Loader wildcards are helpful when there are more routes that your module defines. For example, the Node module uses them because the routes it defines using wildcards are node/%node, node/%node/edit, and node/%node/delete. Instead of putting the code to load the node in 3 different page callbacks, it puts the code in a single function (node_load()
) automatically called from Drupal.