Hi,
I've found a method to implement Dynamic Route in CakePHP.
If we develop a CMS program using CakePHP, we often let people to
define their menu code. The menu code basically is the URL path.
For example: An admin create a menu named 'Milk Product', and give
this menu a code 'product'.
He wish to access this menu via /product
So we can add a static route in routes.php
Router::connect('/product', array('controller' => 'product', 'action'
=> 'index'));
But one day, he want to change the menu code from 'product' to
'milkproduct'? What to do then? Ask the site admin to edit the
routes.php? Of course not.
We can use the code below to resolve this problem.
[CODE]
$menus = '';
$cache = ClassRegistry::init('cache');
$cache->delete('routemenus');
if($menus = $cache->read('routemenus') === false){
$menusModel = ClassRegistry::init('Menu');
$menus = $menusModel->find('all', array('conditions' => array
('parent_id' => '1')));
$cache->write('routemenus', $menus);
}
foreach($menus as $menuitem){
Router::connect('/' . $menuitem['Menu']['code'] . '/:action/*',
array('controller' => $menuitem['MenuType']['code'], 'action' =>
'index'));
}
Router::connect('/', array('controller' => 'homepage', 'action' =>
'index'));
[/CODE]
We have 2 menu related tables: menus and menu_types.
Their structure as belows:
menus
id
menu_type_id
parent_id
lft
rght
code
name
created
modified
menu_types
id
code
name
created
modified
The code in menu_types means the controller name, and the code in
menus means the path.
Check out the new CakePHP Questions site http://cakeqs.org and help others with
their CakePHP related questions.
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en