console command failed [SOLVED]

im using advanced template, i didnt mention because i thought console is same in both,sorry for that.

i moved my file back and change my name space following this link to console\controllers

now my action executed but didn’t know ‘Yii::$app->authManager;’

got this error:




C:\xampp\htdocs\rbac>yii rbac/init

PHP Fatal error:  Call to a member function createPermission() on a non-object i

n C:\xampp\htdocs\rbac\console\controllers\RbacController.php on line 15

PHP Stack trace:

PHP   1. {main}() C:\xampp\htdocs\rbac\yii:0

PHP   2. yii\base\Application->run() C:\xampp\htdocs\rbac\yii:31

PHP   3. yii\console\Application->handleRequest() C:\xampp\htdocs\rbac\vendor\yi

isoft\yii2\base\Application.php:375

PHP   4. yii\console\Application->runAction() C:\xampp\htdocs\rbac\vendor\yiisof

t\yii2\console\Application.php:137

PHP   5. yii\base\Module->runAction() C:\xampp\htdocs\rbac\vendor\yiisoft\yii2\c

onsole\Application.php:161

PHP   6. yii\console\Controller->runAction() C:\xampp\htdocs\rbac\vendor\yiisoft

\yii2\base\Module.php:455

PHP   7. yii\base\Controller->runAction() C:\xampp\htdocs\rbac\vendor\yiisoft\yi

i2\console\Controller.php:91

PHP   8. yii\base\InlineAction->runWithParams() C:\xampp\htdocs\rbac\vendor\yiis

oft\yii2\base\Controller.php:151

PHP   9. call_user_func_array() C:\xampp\htdocs\rbac\vendor\yiisoft\yii2\base\In

lineAction.php:55

PHP  10. console\controllers\RbacController->actionInit() C:\xampp\htdocs\rbac\v

endor\yiisoft\yii2\base\InlineAction.php:55

PHP Fatal Error 'yii\base\ErrorException' with message 'Call to a member functio

n createPermission() on a non-object'


in C:\xampp\htdocs\rbac\console\controllers\RbacController.php:15


Stack trace:

#0 C:\xampp\htdocs\rbac\vendor\yiisoft\yii2\base\InlineAction.php(55): ::call_us

er_func_array()

#1 C:\xampp\htdocs\rbac\vendor\yiisoft\yii2\base\Controller.php(151): yii\base\I

nlineAction->runWithParams()

#2 C:\xampp\htdocs\rbac\vendor\yiisoft\yii2\console\Controller.php(91): yii\base

\Controller->runAction()

#3 C:\xampp\htdocs\rbac\vendor\yiisoft\yii2\base\Module.php(455): yii\console\Co

ntroller->runAction()

#4 C:\xampp\htdocs\rbac\vendor\yiisoft\yii2\console\Application.php(161): yii\ba

se\Module->runAction()

#5 C:\xampp\htdocs\rbac\vendor\yiisoft\yii2\console\Application.php(137): yii\co

nsole\Application->runAction()

#6 C:\xampp\htdocs\rbac\vendor\yiisoft\yii2\base\Application.php(375): yii\conso

le\Application->handleRequest()

#7 C:\xampp\htdocs\rbac\yii(31): yii\base\Application->run()

#8 {main}


C:\xampp\htdocs\rbac>



actually this line fail:




$createPost = $auth->createPermission('createPost');



People, inside advanced tempalate you do not use "app" in your namespace, that is for basic template. In advanced it goes like this: for frontend part is starts with "frontend", for example "frontend/models", "frontend/controllers". For backend app it starts with "backend", in common folder it starts with "common", in console it starts with "console", you do not use "app".

Do you have rbac tables in your database ? If you are using yii\rbac\DbManager… There is nothing wrong with that code.

ok, got

what should i use instead of this line of code?


$auth = Yii::$app->authManager;

should i setting some config? in where?

notice i dont want to use rbac in only frontend or backend, i want to use rbac in my whole project(both front and back)

That line of code is good.

Soon I will release improved advanced template with more features, and rbac installed out of box with very simple and nice ways of using it in your code/app. Untill then, please read this guide: http://www.yiiframework.com/doc-2.0/guide-security-authorization.html , and if you do not understand something there, tell me.

In short:

Inside your config file (common/config/main.php) as a part of components array, you put this line of code:




'authManager' => [

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

],



Then you use yii migration that will install rbac tables to your database ( you invoke it from your console ):




yii migrate @yii/rbac/migrations/



After that you init your RbacController that will populate these rbac tables in your database with roles, permissions and all (you create roles and permissions in your RbacController, that is what will be inserted to your rbac tables).

And last step is to use this in your code like :




if (\Yii::$app->user->can('createPost')) {

    // create post

}



For example if you put this line of code inside post/create action, only users that have this createPost permission will be able to create posts.

yes, every thing is fine about that by using this command:




yii migrate --migrationPath=@yii/rbac/migrations/



Copy and paste this to your console and execute:




yii migrate @yii/rbac/migrations/

everything done by your help and my problem solved, thanks so so so much man

thanks, hope it will be release as soon as possible

actually it was my next Q :P , thanks for mention

again thanks for ur time and ur help

No problem, I am glad I helped.

Hi Guys… I’m having trouble with RBAC…

I’ve set it up as described above and the database tables have been created etc.

However, when I try to execute




...

if(Yii::$app->user->can('admin')){

...



I keep receiving this error from Yii

Please help me out… it seems to happen on all functions of authManager…

I figured that it works in some cases if I use


$auth = new DbManager; 

instead of


$auth = /Yii::$app->authManager;

.

But obviously I can’t do that for most cases with checkAccess.

Thanks

Don’t worry.

I’ve solved the issue.

I had




...

'components' => [

        'authManager' => [

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

        ],

...



in console.php instead of web.php

hi @Nenad, I am using basic template and yii\rbac\PhpManager, when I run this command:




    php yii rbac/init



It give me this error:




    Call to a member function createPermission() on null



Could you help me out with this error?

I never used PhpManager, but from what I see you haven’t configured it well. Read the Configuring RBAC Manager section of this guide very carefully and make sure that you have configured everything right. Your RbacController either do not know about PhpManager or where to store authorization data. Read the Tip: part of this guide section very carefully.

Yes I have read it already. I already have configured the authManager in /config/web.php. And also, have already manually created the three files based on the Tips part as well. I created a rbac folder in my app directory, and those three files have write permission to ‘Others’ already.

Tip: By default, yii\rbac\PhpManager stores RBAC data in three files: @app/rbac/items.php, @app/rbac/assignments.php and @app/rbac/rules.php. Make sure these files are writable by the Web server process if the authorization needs to be changed online. Sometimes you will need to create these files manually.

Under my /config/web.php, my ‘authManager’ component is as below:




'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

],



I checked them all but still same error. ):

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

                

        ],