. I am using the basic template. Do you know the reason? I am using PhpManager
My RbacController is
<?php
namespace app\commands;
use Yii;
use yii\console\Controller;
class RbacController extends Controller
{
public function actionInit()
{
$auth = Yii::$app->authManager;
// add "createPost" permission
$createPost = $auth->createPermission('createPost');
$createPost->description = 'Create a post';
$auth->add($createPost);
// add "updatePost" permission
$updatePost = $auth->createPermission('updatePost');
$updatePost->description = 'Update post';
$auth->add($updatePost);
// add "author" role and give this role the "createPost" permission
$author = $auth->createRole('author');
$auth->add($author);
$auth->addChild($author, $createPost);
// add "admin" role and give this role the "updatePost" permission
// as well as the permissions of the "author" role
$admin = $auth->createRole('admin');
$auth->add($admin);
$auth->addChild($admin, $updatePost);
$auth->addChild($admin, $author);
// Assign roles to users. 1 and 2 are IDs returned by IdentityInterface::getId()
// usually implemented in your User model.
$auth->assign($author, 2);
$auth->assign($admin, 1);
}
}
PHP Fatal error: Call to a member function createPermission() on a non-object in /vagrant/web/y2/commands/RbacController.php on line 14
PHP Fatal Error 'yii\base\ErrorException' with message 'Call to a member function createPermission() on a non-object'
And was because I configured the "authManager" in conf/web.php but I was using the console snippet from The Definitive Guide to Yii 2.0. Solved including the authManager section in conf/console.php:
I also have problems with the yii rbac/init call in the console.
The migration is done and all tables are created but without the initialisation they stay empty.
Because the other posts are old is maby something that changed since than?
I am using advanced template. The web page is runing but after loging out I can not log in again. Perhapse becase there are no permissions set in the tables.
I think it would be really helpful if the documentation was elaborated a bit on what exactly is happening behind the scenes when command "yii rbac/init" is run.
I ran this command and seems it works OK (although also I had trouble with namespace of the advanced framework I am using - why isn’t this in the guide docs? "If you are using advanced template, use namespace “console/controllers” … easy to add, stops a lot of confusion/problems)
So after running yii rbac/init console command, I now have /console/rbac folder with ‘assignments.php’ and ‘items.php’ files inside.
But these files need to be used by both my backend and frontend systems (not console, where I generated them). How do I enable this?
Extra config necessary in the ‘authManager’ component?
I second what gvanto says. I’ve spent half a day working on an issue with this same error message. I’d like a place to add my findings so that others who might be as stupid as me will learn from my mistakes
I’m going to share my solution here for lack of a better place but can I ask the powers that be, can we please add comments or solutions to the end of their ‘The Definitive Guide’ articles just like on php.net - it would be a great resource!
In the end my issue was that I had my DB connection string in main-local.php for frontend and another for backend as they both use two different dbs. When I added a db config to the main-local.php in common it worked! I presume because console couldn’t connect to a db. It would be nice if it gave that error instead of Error: Unknown command “rbac/init”.