console command failed [SOLVED]

hi there

i have the following code int the ‘console\controllers\’




<?php

namespace app\console;


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

      ....

      ....

      ....

      ....


   }

}



but the following error occurred while running ‘yii rbac’ from root of project:




C:\xampp\htdocs\rbac>yii rbac

Exception 'yii\base\UnknownClassException' with message 'Unable to find 'console

\controllers\RbacController' in file: C:\xampp\htdocs\rbac/console/controllers/R

bacController.php. Namespace missing?'


in C:\xampp\htdocs\rbac\vendor\yiisoft\yii2\BaseYii.php:291


Stack trace:

#0 [internal function]: yii\BaseYii::autoload('console\control...')

#1 [internal function]: spl_autoload_call('console\control...')

#2 C:\xampp\htdocs\rbac\vendor\yiisoft\yii2\base\Module.php(563): class_exists('

console\control...')

#3 C:\xampp\htdocs\rbac\vendor\yiisoft\yii2\base\Module.php(521): yii\base\Modul

e->createControllerByID('rbac')

#4 C:\xampp\htdocs\rbac\vendor\yiisoft\yii2\base\Module.php(449): yii\base\Modul

e->createController('rbac')

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

se\Module->runAction('rbac', Array)

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

nsole\Application->runAction('rbac', Array)

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

le\Application->handleRequest(Object(yii\console\Request))

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

#9 {main}


C:\xampp\htdocs\rbac>




You have to run:


yii rbac/init

not:


yii rbac

In your code your are invoking rbac controller, but you also need to say which action you want to run, in this case "init" one.

thanks man

by running rbac/init, got this error:




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

Exception 'yii\base\UnknownClassException' with message 'Unable to find 'console

\controllers\RbacController' in file: C:\xampp\htdocs\rbac/console/controllers/R

bacController.php. Namespace missing?'


in C:\xampp\htdocs\rbac\vendor\yiisoft\yii2\BaseYii.php:291


Stack trace:

#0 [internal function]: yii\BaseYii::autoload('console\control...')

#1 [internal function]: spl_autoload_call('console\control...')

#2 C:\xampp\htdocs\rbac\vendor\yiisoft\yii2\base\Module.php(563): class_exists('

console\control...')

#3 C:\xampp\htdocs\rbac\vendor\yiisoft\yii2\base\Module.php(521): yii\base\Modul

e->createControllerByID('rbac')

#4 C:\xampp\htdocs\rbac\vendor\yiisoft\yii2\base\Module.php(449): yii\base\Modul

e->createController('rbac/init')

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

se\Module->runAction('rbac/init', Array)

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

nsole\Application->runAction('rbac/init', Array)

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

le\Application->handleRequest(Object(yii\console\Request))

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

#9 {main}


C:\xampp\htdocs\rbac>




are my namespace and … right?

Oh yes, your namespace should be:


app/commands

It seems that folder structure has been changed since the guide has beed written.

EDIT: And you have to put your RbacController inside commands folder. You will have that folder when you install basic template.

thanks nenad for reply

i changed my namespace and created a folder named commands in the root(next to backend and … folders)

ran the rbac/init and got this error:


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

Error: Unknown command "rbac/init".


C:\xampp\htdocs\rbac>

Are you using basic or advanced template ? Guide you are following is made for basic template, you can not use it as-is for developing with advanced template. I tought u are using basic. If you are using advanced tempalte, you need to put your RbacController inside console/controllers folder, and your namespace will be: console/controllers. "app" is root namespace for basic template not for advanced. I tought you know that…

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