We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Invo navbar ignoring multiple paths to same controller

Hello!

In the elements.php file I want a dropdown menu to the profile index, and edit profile. However it skips the index and only shows the edit profile.

If I rename the index profile to say profile2, both shows. However of course it calls index on a profile2 controller and fails.

How do I call two different Actions in the same controller in the same dropdown? It works if I have one on the left bar and one on the right bar, but not in the same side. It just overwrites the previous one.


class Elements extends Component {
    private $_headerMenu = array(
        'navbar-left' => array(
            'conversation' => array(
                'caption' => 'Convos',
                'action' => 'index',
            ),
        ),
        'navbar-right' => array(
            'dropdown' => array(
                'profile' => array(
                    'caption' => 'Index',
                    'action' => 'index',
                ),
                'profile' => array(
                    'caption' => 'Edit Profile',
                    'action' => 'edit',
                ),
            ),
            'session' => array(
                'caption' => 'Log In/Sign Up',
                'action' => 'index',
            ),
        ),
    );

    /**
     * Builds header menu with left and right items
     *
     * @return string
     */
    public function getMenu() {

        $auth = $this->session->get('auth');
        if ($auth) {
            $this->_headerMenu['navbar-right']['session'] = array(
                'caption' => 'Log Out',
                'action' => 'end',
            );
        } else {
            unset($this->_headerMenu['navbar-left']['invoices']);
            unset($this->_headerMenu['navbar-left']['profile']);
            unset($this->_headerMenu['navbar-left']['hangouts']);
            unset($this->_headerMenu['navbar-right']['profile']);
        }

        $controllerName = $this->view->getControllerName();
        foreach ($this->_headerMenu as $position => $menu) {
            echo '<div class="nav-collapse">';
            echo '<ul class="nav navbar-nav ', $position, '">';
            foreach ($menu as $controller => $option) {
                if ($controllerName == $controller) {
                    echo '<li class="active">';
                } else {
                    echo '<li>';
                }
                if ($controller == 'dropdown') {
                    echo '
                <li class="dropdown">
                  <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                    Events<span class="caret"></span>
                </a>
                <ul class="dropdown-menu" role="menu">';
                    foreach ($option as $dropcontroller => $dropoption) {
                        echo '<li>';
                        echo $this->tag->linkTo($dropcontroller . '/' . $dropoption['action'], $dropoption['caption']);
                        echo '</li>';
                    }
                    echo '</ul>';
                } else {
                    echo $this->tag->linkTo($controller . '/' . $option['action'], $option['caption']);
                    echo '</li>';
                }
            }
            echo '</ul>';
            echo '</div>';
        }
    }
}

Thank you!!

The reason that doesn't work, is because $_headerMenu is an array and you can't define 2 indexes with the same name. Your best bet is to put the dropdown elements as arrays and change the getMenu function.

'dropdown' => array(
        array(
            'profile' => array(
                'caption' => 'Index',
                'action' => 'index',
            )
        ),
        array(
            'profile' => array(
                'caption' => 'Edit Profile',
                'action' => 'edit',
            )
        )
)