Echo执行了两次

时间:2017-01-13 22:47:40

标签: php phalcon

我已经在我的网站上添加了一个ACL,但是当我在SecurityPlugin.php文件中测试我的角色变量的结果时,我得到了两次结果。

为什么Phalcon会两次显示$ role的var_dump?我对这个框架相当新,我最初的想法是,由于在Phalcon中路由,我可能会这样做吗?

Phalcon版本:3.0.3

\应用\插件\ SecurityPlugin.php     

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

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 = [
    'admins' => new Role(
      'admins',
      'Website administrators'
    ),
            '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(
            'account'    => array('*')
        );

  $privateResourcesAdmin = array(
            'admin'     => array('*')
        );

  //Public area resources
        $publicResources = array(
            'index'       => array('*'),
            'register'    => array('*'),
            'errors'      => array('show401', 'show404', 'show500'),
            'register'    => array('*'),
            'login'       => array('*'),
            'logout'            => array('*')
        );

        foreach ($privateResources as $resource => $actions) {
            $acl->addResource(new Resource($resource), $actions);
        }

  foreach ($privateResourcesAdmin as $resource => $actions) {
    $acl->addResource(new Resource($resource), $actions);
  }

        foreach ($publicResources as $resource => $actions) {
            $acl->addResource(new Resource($resource), $actions);
        }

        //Grant access to public areas to users, admins 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);
            }
        }

  foreach ($privateResourcesAdmin as $resource => $actions) {
            foreach ($actions as $action){
                $acl->allow('admins', $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 beforeExecuteRoute(Event $event, Dispatcher $dispatcher){
    $auth = $this->session->get('auth');

    if (!$auth){
        $role = 'guests';
    } else {
  if ($this->session->has("account_type")) {
      $type = $this->session->get("account_type");
      if($type == 99){
        $role = 'admins';
      } else {
        $role = 'users';
      }
  }
    }

    var_dump($role);

    $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) {
        $dispatcher->forward(array(
            'controller' => 'errors',
            'action'     => 'show401'
        ));
  $this->session->destroy();
        return false;
    }
}
}

\公共\的index.php

<?php
use Phalcon\Di\FactoryDefault;
use Phalcon\Mvc\Dispatcher; //Used for ACL list and authorization routing
use Phalcon\Events\Manager as EventsManager; //Used for ACL List
use Phalcon\Mvc\Router; //Used for routing logout page

error_reporting(E_ALL);

define('BASE_PATH', dirname(__DIR__));
define('APP_PATH', BASE_PATH . '/app');

try {

    /**
     * The FactoryDefault Dependency Injector automatically registers
     * the services that provide a full stack framework.
     */
    $di = new FactoryDefault();

    /**
     * Read services
     */
    include APP_PATH . "/config/services.php";

    /**
     * Get config service for use in inline setup below
     */
    $config = $di->getConfig();

    /**
     * Include Autoloader
     */
    include APP_PATH . '/config/loader.php';

    //This makes sure the routes are correctly handled for authorized/unauthorized in people
    /**
     * MVC dispatcher
     */
    $di->set("dispatcher", function () use ($di) {
      // Create an events manager
       $eventsManager = $di->getShared('eventsManager');

       /**
       *Check if the user is allowed to access certain action using the SecurityPlugin
       *Listen for events produced in the dispatcher using the Security plugin
       */
       $eventsManager->attach(
           "dispatch:beforeExecuteRoute",
           new SecurityPlugin()
       );

      // Handle exceptions and not-found exceptions using NotFoundPlugin
       $eventsManager->attach(
           "dispatch:beforeException",
           new NotFoundPlugin()
       );

       $dispatcher = new Dispatcher();

       // Assign the events manager to the dispatcher
       $dispatcher->setEventsManager($eventsManager);

       return $dispatcher;
      }
    );

    /**
     * Handle and deploy the application
     */
    $application = new \Phalcon\Mvc\Application($di);

    echo $application->handle()->getContent();



} catch (\Exception $e) {
    echo $e->getMessage() . '<br>';
    echo '<pre>' . $e->getTraceAsString() . '</pre>';
}

1 个答案:

答案 0 :(得分:1)

因为你正在向前迈进 - 所以这意味着还有其他行动再次执行,beforeExecuteRoute再次被解雇 - 这就是为什么2次var_dump