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.

"Parameter 'name' must be a string" after upgrade from Phalcon 1.3 to 2.0.1

Hi guys,

after upgarding from 1.3 to 2.0 my ACL Roles plugin is not working anymore and throws a Exception with the message "Parameter 'name' must be a string"".

This is my ACL role plugin class which worked with 1.3:

namespace Easy\Plugins;

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

class AclRoles extends Plugin
{
    /**
     * @type int
     */
    const ACL_R_ANONYMOUS = 0;

    /**
     * @type int
     */
    const ACL_R_USERS = 1;

    /**
     * ACL rules gets checked before a controller gets dispatched.
     *
     * @param Event $event
     * @param Dispatcher $dispatcher
     * @return bool
     */
    public function beforeExecuteRoute(Event $event, Dispatcher $dispatcher)
    {
        $auth = $this->session->get('user-id');
        $role = $auth ? self::ACL_R_USERS : self::ACL_R_ANONYMOUS;

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

        $acl = $this->buildAcl();

        if (!$acl->isAllowed($role, $controller, $action)) {
            $this->flash->error('ACL Rules does not match!');

            $dispatcher->forward([
                'controller' => 'account',
                'action'     => 'login'
            ]);

            return false;
        }

        return true;
    }

    /**
     * Builds the ACL rules.
     *
     * @return \Phalcon\Acl\Adapter
     */
    private function buildAcl()
    {
        $acl = new Memory();
        $acl->setDefaultAction(Acl::DENY);

        $acl->addRole(new Role(self::ACL_R_ANONYMOUS));
        $acl->addRole(new Role(self::ACL_R_USERS));

        $resourceAccount = new Resource('account');
        $acl->addResource($resourceAccount, 'login');
        $acl->addResource($resourceAccount, 'register');

        $acl->addResource(new Resource('error'), '*');

        $acl->allow(self::ACL_R_USERS, '*', '*');
        $acl->allow(self::ACL_R_ANONYMOUS, 'account', 'login');
        $acl->allow(self::ACL_R_ANONYMOUS, 'account', 'register');
        $acl->allow(self::ACL_R_ANONYMOUS, 'error', '*');

        return $acl;
    }
}

The error occurs first when calling $acl->setDefaultAction(Acl::DENY); , but according to the latest dev tools the set default action method has one integer parameter.

And this is the part in my service.php where the plugin gets assigned:

$di->setShared('dispatcher', function() use ($di) {
    /** @var \Phalcon\Events\ManagerInterface $eventsManager */
    $eventsManager = $di->getShared('eventsManager');

    $eventsManager->attach('dispatch', new \Easy\Plugins\AclRoles($di));
    $eventsManager->attach('dispatch', new \Easy\Plugins\ErrorHandler($di));

    $dispatcher = new Phalcon\Mvc\Dispatcher();
    $dispatcher->setEventsManager($eventsManager);
    $dispatcher->setDefaultNamespace('Easy\Controller');

    return $dispatcher;
});

I hope someone has got an Idea or can help me how to debug better with pahlcon.



33.4k
Accepted
answer
edited May '15

The ACL is requiring a string as role name, you can change your constants to:

/**
     * @type int
     */
    const ACL_R_ANONYMOUS = "0";

    /**
     * @type int
     */
    const ACL_R_USERS = "1";

Thanks this solved the error.