PHP code example of ray / role-module

1. Go to this page and download the library: Download ray/role-module library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ray / role-module example snippets


class AppRoleProvider implements RoleProviderInterface
{
    public function get()
    {
        return 'admin';
    }
}

use Ray\RoleModule\ZendAclModule;
use Ray\Di\AbstractModule;
use Laminas\Permissions\Acl\Role\GenericRole;
use Laminas\Permissions\Acl\Acl;

class AppModule extends AbstractModule
{
    protected function configure()
    {
        // @see http://framework.zend.com/manual/current/en/modules/zend.permissions.acl.intro.html
        $acl = new Acl();
        $roleGuest = new GenericRole('guest');
        $acl->addRole($roleGuest);
        $acl->addRole(new GenericRole('staff'), $roleGuest);
        $acl->addRole(new GenericRole('editor'), 'staff');
        $acl->addRole(new GenericRole('administrator'));
        $this->install(new ZendAclModule($acl, AppRoleProvider::class));
    }
}

use Ray\RoleModule\Annotation\RequiresRoles;

#[RequiresRoles(['admin'])]
class Foo
{
    public function createUser($id)
    {

use Ray\RoleModule\Annotation\RequiresRoles;

class Foo
{
   
    #[RequiresRoles(['admin', 'editor'])]
    public function createUser($id)
    {

$ php demo/run.php
// It works!