Automatic routing for page aliases in CakePHP

久未见 提交于 2019-12-06 14:14:45

You can fetch the aliases from the database and put them in routes. This implementation uses caching to prevent loading the routes on every request.

$menus = ''; 
//Cache::delete('routemenus'); You can uncomment this to delete cache if you change menus 
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'));

http://bakery.cakephp.org/articles/iworm/2010/01/10/how-to-implement-dynamic-route-in-cakephp

We need to check if the page_alias is not the original controller we intended for E.g. if you have StatesController than /states/index should refer to index function rather than a states page_alias . For this you will need to ignore the slugs with controller name or already defined route bases when saving.

Next you will have to identify that if page_alias slug exists You can extend CakeRoute for that.

Check out this http://mark-story.com/posts/view/using-custom-route-classes-in-cakephp It has a very better implementation of things you want to do .

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!