0

我正在尝试使用 Entrust 在 Laravel 5.5 上应用角色和权限。

我用这个,作为里面的文档:App\models\Role.php

<?php namespace App;

use Zizaco\Entrust\EntrustRole;

class Role extends EntrustRole
{

    $owner = new Role();
    $owner->name         = 'owner';
    $owner->display_name = 'Project Owner'; // optional
    $owner->description  = 'User is the owner of a given project'; // optional
    $owner->save();

    $admin = new Role();
    $admin->name         = 'admin';
    $admin->display_name = 'User Administrator'; // optional
    $admin->description  = 'User is allowed to manage and edit other users'; // optional
    $admin->save();

    $manager = new Role();
    $manager->name         = 'manager';
    $manager->display_name = 'Company Manager'; // optional
    $manager->description  = 'User is a manager of a Department'; // optional
    $manager->save();
}

?>

但是,当我尝试将角色分配给收到此问题的用户时:

{
..
$owner = new Role();
$owner->name = 'owner';
...
}

"Parse error: syntax error, unexpected '$owner' (T_VARIABLE), expecting function (T_FUNCTION) or const (T_CONST)"

你能给我建议吗?

4

1 回答 1

1

函数/方法怎么样?;)

<?php namespace App;

use Zizaco\Entrust\EntrustRole;

class Role extends EntrustRole
{

public function setup() 
{
    $owner = new Role();
    $owner->name         = 'owner';
    $owner->display_name = 'Project Owner'; // optional
    $owner->description  = 'User is the owner of a given project'; // optional
    $owner->save();

    $admin = new Role();
    $admin->name         = 'admin';
    $admin->display_name = 'User Administrator'; // optional
    $admin->description  = 'User is allowed to manage and edit other users'; // optional
    $admin->save();

    $manager = new Role();
    $manager->name         = 'manager';
    $manager->display_name = 'Company Manager'; // optional
    $manager->description  = 'User is a manager of a Department'; // optional
    $manager->save();
}
}
于 2017-11-08T10:17:39.643 回答