我有多個模型,我想將變量$section
添加到,然後在sidenav.ctp
中使用該值來動態更改邊欄。例如,我的型號有:如何在CakePHP模型中設置變量並在視圖中訪問它?
class Resource extends AppModel {
public $section = 'section1';
public $displayField = 'name';
public $order = 'modified DESC';
//validation, relationships, etc.
}
然後,我有另一種模式,如:
class Topic extends AppModel {
public $section = 'section2';
public $tablePrefix = '';
//validation, relationships, etc.
}
所以在sidenav.ctp
我想要做的事,如:
<?php if ($this->section == 'section1') { ?>
<li><?php echo $this->Html->link(__('Resources'), array('controller' => 'resources', 'action' => 'index')); ?></li>
<li><?php echo $this->Html->link(__('Topics'), array('controller' => 'topics', 'action' => 'index')); ?> </li>
<li><?php echo $this->Html->link(__('Log Out'), array('controller' => 'users', 'action' => 'logout')); ?> </li>
<?php } ?>
<?php if ($this->section == 'section2') { ?>
<li><?php echo $this->Html->link(__('Resources1'), array('controller' => 'resources', 'action' => 'index')); ?></li>
<li><?php echo $this->Html->link(__('Topics1'), array('controller' => 'topics', 'action' => 'index')); ?> </li>
<li><?php echo $this->Html->link(__('Log Out1'), array('controller' => 'users', 'action' => 'logout')); ?> </li>
<?php } ?>
但訪問$section
這樣不起作用。我無法弄清楚如何在模型中設置值,然後在視圖中訪問它。我知道我可以在控制器中設置值,然後通過$section
訪問它,但這需要我將它放在每個函數中。
爲什麼模型?我認爲沒有必要。 – Salines 2015-02-05 21:30:14
如果我將它放在控制器中,我將如何設置它,因此我不需要將它分別放在每個函數中? – WOUNDEDStevenJones 2015-02-05 21:31:14
你會爲不同的模型顯示不同的菜單? – Salines 2015-02-05 21:34:26