2012-11-08 23 views
0

我是CakePHP的新手,我需要您的幫助。我需要在特定頁面上顯示特定的窗口小部件,這些窗口小部件是我的主頁,並禁用其餘頁面。基本上我已經能夠明確顯示基於日誌中的地位特殊的div如下所示:根據所選擇的頁面在CakePHP中禁用特定的div

  <?php if (!$this->Session->read('Auth.User.id')): ?> 
       <div class="register link right <?php if ($active == 'register') echo 'active'; ?>"><?php echo $html->link('Register', array('controller' => 'users', 'action' => 'register')); ?></div> 
       <div class="login link right <?php if ($active == 'login') echo 'active'; ?>"><?php echo $html->link('Login', array('controller' => 'users', 'action' => 'login')); ?></div> 
      <?php else: ?> 
       <div class="logout link right"><?php echo $html->link('Logout', array('controller' => 'users', 'action' => 'logout')); ?></div> 
       <div class="myaccount link right <?php if ($active == 'myaccount') echo 'active'; ?>"><?php echo $html->link('My account', array('controller' => 'account', 'action' => 'summary')); ?></div> 
      <?php endif; ?> 

我是問任何幫助,關於顯示基於我的主頁中選擇一個特定的div。

下面的僞代碼表示我的想法行我修來解決這個問題:

<?php if (the selected page is homepage or default.ctp)?> 
    // set the display property for the desired div to none 
<?php else: ?> 
    // do not set the display property for the desired div to none 
<?php endif; ?> 
+2

你的代碼有什麼問題? –

+0

嗨,亞歷山德羅,謝謝你的回覆。爲什麼我無法確定在if語句括號內選擇了哪個視圖,我基本上失去了信心。我編輯了我的文章並添加了僞代碼來表示我的思路。另外我相信,一旦我確定頁面動態需要設置div的顯示樣式屬性,我打算禁用爲無。這在cakePHP中是如何動態實現的?謝謝 – TokTok123

回答

1

在CakePHP你不能直接$this->Session->read('Auth.User.id')在視圖中使用的是bettere做的到控制器中這樣的:

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

和你的觀點

if (!$authUser) 
{ 
    //not logged 
} 
else{ 
    //logged 
} 

而且如果y ou想檢查哪一頁是你可以嘗試類似的東西

echo Router::url($this->last, true); 

是你想要的嗎?

0

在你的控制器,你可以定義是這樣的:

$this->set('pageName', $pageName); 

然後你就可以在您的視圖做:

$class=''; 
if($pageName=='homepage') { 
    $class=' hide'; 
} 
echo $this->Html->div($class, 'your content here'); 

而且想想爲什麼你需要這樣的結構視圖。如果視圖不需要,也許你可以不提供內容?所以你在你的控制器中做出決定。這使得大部分時間視圖更清晰,視圖中需要的數據量最小。

+0

嗨Luc。我真的很感激你的回覆。我會嘗試一下併發布消息 – TokTok123

相關問題