How to: Implement Dynamic Route in CakePHP

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$menus = '';
Cache::delete('routemenus');
if($menus = Cache::read('routemenus') === false){
    echo 'load from db';
    $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'));

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.

Share
This entry was posted in 编程 and tagged , , , . Bookmark the permalink.

2 Responses to How to: Implement Dynamic Route in CakePHP

  1. Four eyes says:

    想当程序员的摄影师

  2. 想打摄影师的程序员 says:

    想打摄影师的程序员

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">