1

私はcakephp2で開発されたサイトを持っていて、それをdefault.ctpに入れたいのですが、データベースから取得したいくつかの要素を含むメニューがあります。使用する必要のあるコントローラーの要素を変更するにはどうすればよいですか?クエリをどこに置く必要がありますか?モデルに興味があるのは簡単ですが、default.ctpに興味があるからです。どうやってやるの?これが私のdefault.ctpです:

<body>
        <div class="content">
            <div id="navigation">
                <ul>


                    <?php
                        if (!empty($authUser)) {
                        ?>
                        <li><?php echo $this->Html->link('I want to change this',   array('controller' => 'ingredients',    'action' => 'index')); ?></li>
                        <li><?php echo $this->Html->link('I want to change this',       array('controller' => 'brands',         'action' => 'index')); ?></li>
                        <li><?php echo $this->Html->link('I want to change this',   array('controller' => 'manufacturers',  'action' => 'index')); ?></li>
                    <?php
                            // $is_logged from UsersController->beforeFilter
                            echo $this->element ('header_menu_logged');
                        } else {
                            ?>
                            <li><?php echo $this->Html->link('Competizioni',        array('controller' => 'brands',         'action' => 'index')); ?></li>
                            <li><?php echo $this->Html->link('Cerca',   array('controller' => 'manufacturers',  'action' => 'index')); ?></li>
                            <?php
                            // $current_model
                            echo $this->element ('header_menu', array('selected' => 'Pippo'));
                        }
                    ?>
                </ul>
            </div>
            <div class="page">
                <?php
                // messaggi di stato per le azioni

                if (empty($flash_element)) {
                    $flash_element = $this->Session->read('flash_element');
                    if (empty($flash_element)) {
                        $flash_element = 'default';
                    }
                }

                // echo '>'.$flash_element.'<';

                $auth_msg = $this->Session->flash('auth', array ('element' => 'flash_'.$flash_element));
                $flash_msg = $this->Session->flash('flash', array ('element' => 'flash_'.$flash_element));

                if (!empty($auth_msg)) {
                    echo $auth_msg;
                }

                if (!empty($flash_msg)) {
                    echo $flash_msg;
                }
                ?>

                <section><!--class="contents"-->
                <?php echo $content_for_layout; ?>
                </section>
            </div>
        </div>
    </body>
4

1 に答える 1

5

さて、私は$this->set('anyString', $varStringOfTheLink);あなたbeforeRender()またはbeforeFilter()appController.phpで行うことをお勧めします。

そこからクエリを実行し、set()関数を使用してビューの変数を設定します。

次に、default.ctpでを使用できるようになります$anyString

簡単なまとめをします。フィルタの前またはレンダリングの前に、appControllerでクエリを実行してから、ビューで使用できるように設定します。関数の最初のパラメーターは、set()ビューで使用する変数の名前です。$前に置くだけです。2番目のパラメーターは、必要な変数の値です。

//appController.php
function beforeFilter(){
    $myQueryVar = $this->Model->find('whateverIWant');
    $this->set('myLinkOne', $myQueryVar);
}

//layouts/default.ctp
echo $this->Html->link($myLinkOne,   array('controller' => 'ingredients',    'action' => 'index'));
于 2012-08-17T18:05:05.390 に答える