0
votes

I'm currently trying to use cakephp to create my own small CMS in php. So that it should be easy to modify the website. First of all im not a web programmer this is just for spare time.

I have a controller Button and a button can have multiple categories. so im trying to load this into the default.ctp so that this menu will be loaded for every page.

default.ctp code:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>
        <?php __('eg'); ?>
        <?php echo $title_for_layout; ?>
    </title>
    <?php
    echo $this->Html->css('cake.generic.style');
    echo $scripts_for_layout;
    ?>
</head>
<body>
    <div class="wrapper">
        <div class="header">
            <h1>Electro Geers Bvba</h1>
        </div>
        <div id="navMenu">
            <?php
            foreach ($buttons as $button) {
                echo '<ul>';
                echo '<li><a href="' . h($button['Button']['naam']) . '.html">' . h($button['Button']['naam']) . '</a>';
                if (!empty($button['Category'])) {
                    echo'<ul>';
                    foreach ($button['Category'] as $category) {
                        echo '<li><a href="' . $category['naam'] . '.html">' . $category['naam'] . '</a></li>';
                    }
                    echo'</ul>';
                }
                echo '</li>';
                echo '</ul>';
            }
            ?>
            <br class="clearFloat" />
        </div>
        <div class="contents">
            <h3>Sectionale poorten en Hekwerken</h3>
            <?php echo $content_for_layout; ?>

        </div>
        <div class="footer">
           Copyright © 2012 - 2017
        </div>
    </div>
</body>

I know that cakephp normaly probaly not should be used like this way but i want to know if it is possible? How can i load the button name into default.ctp?

I am currently getting this error:

Notice (8): Undefined variable: buttons [APP\View\Layouts\default.ctp, line 20]

Warning (2): Invalid argument supplied for foreach() [APP\View\Layouts\default.ctp, line 20]

2

2 Answers

3
votes

In your AppController:

$this->loadModel('Button');
$buttons = $this->Button->find('all');
$this->set(compact('buttons'));

This will cause every page to query the buttons table and pass the results to the view, which will then be accessible via your $buttons variable.

If you're going to be handling menus via the database, I highly suggest looking into caching in one form or another - probably not the best idea to force a database call like this on every page.

There area lot of options, but one that I've seen and like is to retain the menu in an XML file - then, anytime your buttons are updated/edited/added/deleted...etc, update the XML file.

1
votes

You could also possibly put the div id="navMenu" into an element, and then use requestAction. See this link from one of the Cake developers.

http://mark-story.com/posts/view/how-using-requestaction-increased-performance-on-my-site