0

是否有可能在 CakePHP v 2.x 中根据条件修改默认布局 (default.ctp) - 例如用户是否登录。假设我有一个菜单:

“主页”“菜单项 1”“菜单项 2”

一旦用户登录,我想更改它:

“首页” “菜单项 1” “菜单项 2” “菜单项 3” “菜单项 4”

我在哪里/如何实现这一目标?我假设我必须调用“$this->layout('new-layout');” 某处,但我不确定到底在哪里。

谢谢

4

2 回答 2

2

If there are too many difference between your both layout (before / after log in) than make two layout & in your AppController (in beforeFilter function) change layout if user is logged in.

if($this->Auth->user()){
    $this->layout = "new_layout";
}

And if it is just about some menu links. You can directly put condition in your default.ctp file.

if ($this->Session->read('Auth.User')){
 //Add more menu for logged in users.
}
于 2013-04-17T10:50:54.633 回答
0

如果我没记错的话,您应该可以使用相同的布局并直接使用:

-- Menu Item 1
-- Menu Item 2
if ($authUser) {
  -- Menu Item 3
  -- Menu Item 4
}

您必须在控制器中设置以下内容:

$this->set('authUser', $this->Auth->user());

不过我用蛋糕已经有一段时间了!

于 2013-04-17T10:47:08.550 回答