console command failed [SOLVED]

While executing the

I am geting error

. 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);

    }

} 

did you set up this config?




return [

    // ...

    'components' => [

        'authManager' => [

            'class' => 'yii\rbac\PhpManager',

        ],

        // ...

    ],

];



Yes, in web.php

you should set it up in console.php

It created two files

and

in the root directory . Is it correct ?

I added this in console.php


 'authManager' => [

        'class' => 'yii\rbac\PhpManager',

        'itemFile' => 'app\rbac\items.php', //Default path to items.php | NEW CONFIGURATIONS

        'assignmentFile' => 'app\rbac\assignments.php', //Default path to assignments.php | NEW CONFIGURATIONS

        'ruleFile' => 'app\rbac\rules.php', //Default path to rules.php | NEW CONFIGURATIONS

        	

        ],

If I add


'authManager' => [

        'class' => 'yii\rbac\PhpManager',

        'itemFile' => '@app\rbac\items.php', //Default path to items.php | NEW CONFIGURATIONS

        'assignmentFile' => '@app\rbac\assignments.php', //Default path to assignments.php | NEW CONFIGURATIONS

        'ruleFile' => '@app\rbac\rules.php', //Default path to rules.php | NEW CONFIGURATIONS

        	

        ],

it shows an error

Exception ‘yii\base\InvalidParamException’ with message ‘Invalid path alias: @app\rbac\items.php’

in

Do not use app as a subdir of your project because app and @app is the alias of your project which is always defined by yii2.

Try the following settings:


'\rbac\assignments.php'

'\rbac\items.php'

in the root directory.

console.php :


 'authManager' => [

        'class' => 'yii\rbac\PhpManager',

        'itemFile' => 'rbac\items.php', //Default path to items.php | NEW CONFIGURATIONS

        'assignmentFile' => 'rbac\assignments.php', //Default path to assignments.php | NEW CONFIGURATIONS

        'ruleFile' => 'rbac\rules.php', //Default path to rules.php | NEW CONFIGURATIONS

                

        ],

I found the solution. I think, since I am using linux, the correct configuration is


'authManager' => [

        'class' => 'yii\rbac\PhpManager',

        'itemFile' => '@app/rbac/items.php', //Default path to items.php | NEW CONFIGURATIONS

        'assignmentFile' => '@app/rbac/assignments.php', //Default path to assignments.php | NEW CONFIGURATIONS

        'ruleFile' => '@app/rbac/rules.php', //Default path to rules.php | NEW CONFIGURATIONS

        	

        ],

.Now it is working.

Hello there. My 2 cents

I got this error:




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:




 'components' => [

    	'authManager' => [

    		'class' => 'yii\rbac\PhpManager',

    	],

...

]



Then I got this error:




$ ./yii rbac/init


PHP Warning 'yii\base\ErrorException' with message 'file_put_contents(/vagrant/web/y2/rbac/items.php): failed to open stream: No such file or directory'


in /vagrant/web/y2/vendor/yiisoft/yii2/rbac/PhpManager.php:714


Stack trace:

#0 [internal function]: yii\base\ErrorHandler->handleError(2, 'file_put_conten...', '/vagrant/web/y2...', 714, Array)

#1 /vagrant/web/y2/vendor/yiisoft/yii2/rbac/PhpManager.php(714): file_put_contents('/vagrant/web/y2...', '<?php\nreturn [\n...', 2)

#2 /vagrant/web/y2/vendor/yiisoft/yii2/rbac/PhpManager.php(740): yii\rbac\PhpManager->saveToFile(Array, '/vagrant/web/y2...')

#3 /vagrant/web/y2/vendor/yiisoft/yii2/rbac/PhpManager.php(619): yii\rbac\PhpManager->saveItems()

#4 /vagrant/web/y2/vendor/yiisoft/yii2/rbac/BaseManager.php(118): yii\rbac\PhpManager->addItem(Object(yii\rbac\Permission))

#5 /vagrant/web/y2/commands/RbacController.php(18): yii\rbac\BaseManager->add(Object(yii\rbac\Permission))

#6 [internal function]: app\commands\RbacController->actionInit()

#7 /vagrant/web/y2/vendor/yiisoft/yii2/base/InlineAction.php(55): call_user_func_array(Array, Array)

#8 /vagrant/web/y2/vendor/yiisoft/yii2/base/Controller.php(151): yii\base\InlineAction->runWithParams(Array)

#9 /vagrant/web/y2/vendor/yiisoft/yii2/console/Controller.php(91): yii\base\Controller->runAction('init', Array)

#10 /vagrant/web/y2/vendor/yiisoft/yii2/base/Module.php(455): yii\console\Controller->runAction('init', Array)

#11 /vagrant/web/y2/vendor/yiisoft/yii2/console/Application.php(161): yii\base\Module->runAction('rbac/init', Array)

#12 /vagrant/web/y2/vendor/yiisoft/yii2/console/Application.php(137): yii\console\Application->runAction('rbac/init', Array)

#13 /vagrant/web/y2/vendor/yiisoft/yii2/base/Application.php(375): yii\console\Application->handleRequest(Object(yii\console\Request))

#14 /vagrant/web/y2/yii(23): yii\base\Application->run()

#15 {main}



Finally solved manually creating the @app/rbac directory with:


mkdir rbac

Finally the snippet command worked!! :)

Thank you all for your posts and comments. They were very helpful

Hy,

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.

past your error here

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?

All of this info needs to be in the guide docs here (to stop confusion and if yii2 is going to successfully compete with things like Laravel, must have great docs for this great framework!): http://www.yiiframework.com/doc-2.0/guide-security-authorization.html)

Just my 2 cents.

Love yii2 :)

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 :rolleyes:

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”.

1 Like