doumen5895 2013-09-24 21:51
浏览 31
已采纳

cakephp - 用于设置可用导航链接的每个控制器操作的回调函数

I'm trying to achieve something so basic in my cakephp-app, that I'm quite surprised I didn't easily find a solution to it...

What I just want to do is to set available links for my app's main navigation depending on the user being logged in or not and if he is, depending on his role (which is stored in the users-table).

So basically a function like this:

if(!$this->request->is('ajax')) {
    if(_user_is_not_logged_in_) {
        $availableNavItems = array('login','help');
        }
    else {
        if($this->Auth->User('role') == 'user') {
            $availableNavItems = array('something','something else','whatever','help','logout');
        }
        elseif($this->Auth->User('role') == 'admin') {
            $availableNavItems = array('something','something else','whatever','admin-tool','user management','help','logout');
        }
    }
    // set available pages for layout
    $this->set('availableNavItems',$availableNavItems);
}

In my layout of course I would create a navbar with links to those available pages.

The only question I have - where would I put code like the above? Is there any callback-function I could put in AppController which cakephp calls on every request? And, what would be a good way to check what I wrote as pseudo-code "_user_is_not_logged_in_" above?

Thanks in advance for any help!

  • 写回答

1条回答 默认 最新

  • dpxw17759 2013-09-24 21:55
    关注
    if(_user_is_not_logged_in_) {
    

    could be written as

    if(!$this->Auth->user('id')){
    

    And you could put the function in your beforeRender method of your AppController, which executes on every request, right before the view is rendered.

    Also of note is the beforeFilter method, which gets called early, before the controller logic executes. You shouldn't need it in this case, but it's worth knowing about.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测