Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

[Phalcon 3.01 - INVO] I have a questions regarding headmenu in INVO example.

Good day everyone, [Phalcon 3.01 - INVO] Good day everyone. I have a questions regarding headmenu in INVO example.

The original headmenu: 'Home' - 'About' - 'Contact', updated menu: 'Home' - 'About' - 'Myitem', The menu-item of 'Contact' is not found.

I want to add new element(Myitem) on headmenu, app\library\Elements.php that content is following:

<?php

use Phalcon\Mvc\User\Component;

/**

  • Elements
  • Helps to build UI elements for the application */ class Elements extends Component {

    private $_headerMenu = array( 'navbar-left' => array( 'index' => array( 'caption' => 'Home', 'action' => 'index' ), 'invoices' => array( 'caption' => 'Invoices', 'action' => 'index' ), 'about' => array( 'caption' => 'About', 'action' => 'index' ), 'contact' => array( 'caption' => 'Contact', 'action' => 'index' ), 'contact' => array( 'caption' => 'Myitem', 'action' => 'index' ), ), 'navbar-right' => array( 'session' => array( 'caption' => 'Login/Sign Up', 'action' => 'index' ), ) );

    private $_tabs = array( 'Invoices' => array( 'controller' => 'invoices', 'action' => 'index', 'any' => false ), 'Companies' => array( 'controller' => 'companies', 'action' => 'index', 'any' => true ), 'Products' => array( 'controller' => 'products', 'action' => 'index', 'any' => true ), 'Product Types' => array( 'controller' => 'producttypes', 'action' => 'index', 'any' => true ), 'Your Profile' => array( 'controller' => 'invoices', 'action' => 'profile', 'any' => false ) );

    /**

    • 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']); }

      $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>'; } echo $this->tag->linkTo($controller . '/' . $option['action'], $option['caption']); echo '</li>'; } echo '</ul>'; echo '</div>'; }

    }

    /**

    • Returns menu tabs */ public function getTabs() { $controllerName = $this->view->getControllerName(); $actionName = $this->view->getActionName(); echo '<ul class="nav nav-tabs">'; foreach ($this->_tabs as $caption => $option) { if ($option['controller'] == $controllerName && ($option['action'] == $actionName || $option['any'])) { echo '<li class="active">'; } else { echo '<li>'; } echo $this->tag->linkTo($option['controller'] . '/' . $option['action'], $caption), '</li>'; } echo '</ul>'; } } -------- The end

app\plugins\SecurityPlubin.php that content is following: <?php

use Phalcon\Acl; use Phalcon\Acl\Role; use Phalcon\Acl\Resource; use Phalcon\Events\Event; use Phalcon\Mvc\User\Plugin; use Phalcon\Mvc\Dispatcher; use Phalcon\Acl\Adapter\Memory as AclList;

/**

  • SecurityPlugin
  • This is the security plugin which controls that users only have access to the modules they're assigned to */ class SecurityPlugin extends Plugin { /**

    • Returns an existing or new access control list
    • @returns AclList */ public function getAcl() { if (!isset($this->persistent->acl)) {

      $acl = new AclList();
      
      $acl->setDefaultAction(Acl::DENY);
      
      // Register roles
      $roles = [
          'users'  => new Role(
              'Users',
              'Member privileges, granted after sign in.'
          ),
          'guests' => new Role(
              'Guests',
              'Anyone browsing the site who is not signed in is considered to be a "Guest".'
          )
      ];
      
      foreach ($roles as $role) {
          $acl->addRole($role);
      }
      
      //Private area resources
      $privateResources = array(
          'companies'    => array('index', 'search', 'new', 'edit', 'save', 'create', 'delete'),
          'products'     => array('index', 'search', 'new', 'edit', 'save', 'create', 'delete'),
          'producttypes' => array('index', 'search', 'new', 'edit', 'save', 'create', 'delete'),
          'invoices'     => array('index', 'profile')
      );
      foreach ($privateResources as $resource => $actions) {
          $acl->addResource(new Resource($resource), $actions);
      }
      
      //Public area resources
      $publicResources = array(
          'index'      => array('index'),
          'about'      => array('index'),
          'register'   => array('index'),
          'errors'     => array('show401', 'show404', 'show500'),
          'session'    => array('index', 'register', 'start', 'end'),
          'contact'    => array('index', 'send'),
          'myitem'     => array('index')
      );
      foreach ($publicResources as $resource => $actions) {
          $acl->addResource(new Resource($resource), $actions);
      }
      
      //Grant access to public areas to both users and guests
      foreach ($roles as $role) {
          foreach ($publicResources as $resource => $actions) {
              foreach ($actions as $action){
                  $acl->allow($role->getName(), $resource, $action);
              }
          }
      }
      
      //Grant access to private area to role Users
      foreach ($privateResources as $resource => $actions) {
          foreach ($actions as $action){
              $acl->allow('Users', $resource, $action);
          }
      }
      
      //The acl is stored in session, APC would be useful here too
      $this->persistent->acl = $acl;

      }

      return $this->persistent->acl; }

    /**

    • This action is executed before execute any action in the application
    • @param Event $event
    • @param Dispatcher $dispatcher
    • @return bool */ public function beforeDispatch(Event $event, Dispatcher $dispatcher) {

      $auth = $this->session->get('auth'); if (!$auth){ $role = 'Guests'; } else { $role = 'Users'; }

      $controller = $dispatcher->getControllerName(); $action = $dispatcher->getActionName();

      $acl = $this->getAcl();

      if (!$acl->isResource($controller)) { $dispatcher->forward([ 'controller' => 'errors', 'action' => 'show404' ]);

      return false;

      }

      $allowed = $acl->isAllowed($role, $controller, $action); if ($allowed != Acl::ALLOW) { $dispatcher->forward(array( 'controller' => 'errors', 'action' => 'show401' )); $this->session->destroy(); return false; } }



31.2k
Accepted
answer

Hi,


// in app\library\Elements.php, change
        'contact' => array(
            'caption' => 'Myitem',
            'action' => 'index'
        ),
// to
        'myitem' => array(  // here the controller name
            'caption' => 'Myitem',
            'action' => 'index'
        ),

this for getting the security plugin to work properly.

By looking at https://github.com/phalcon/invo/blob/master/app/library/Elements.php#L73, there is no reason that your "Contact" link shouldn't be displayed in your menu.

I would appreciate long explainations, Thanks a lot.



31.2k

I would appreciate long explainations

what kind of expnaination ?