Including My Own Folder Of Components In The Namespace

I am extending some of the Objects in Yii including the DbManager, I don’t know which could be the best practice on where to host my own files under the application structure.

I need this folder to be added to the namespaces that Yii autoloads for them to work well.

Any recomendations ?

I usually put extended components in "components" folder: \common\components (in advanced application template)

Actually I was using the basic template :(. I don’t have a common folder. I don’t have troubles creating a new folder, I just don’t know yet how to include them in the “Autoload” process for the classes and namespace.

Id like to know the same thing. any help would be appreciated!

You don’t need to do anything special. Just create/use a folder and set the appropriate namespace.

For example,

// app\libraries\MyLibrary.php


namespace app\libraries;

class MyLibrary{ }

Then call it with the following:

$myLibrary = new \app\libraries\MyLibrary();

I created a folder in the root called "mycomponents"

So if I have something inside ( a class ) and I create a namespace for it, it does not work.

I can "Include it" the old way, but I wanted to know if there is a proper way to do it.

Can you post the code - how are you defining and calling the class?


I have a class called "MyDbManager" that actually I created under the rbac folder of yii, due that I could not make it work from an other folder. This class should be called in the config file of Yii under components:

        'authManager' => [

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


If I have a folder in the root directory called components and my components I can not access that Class. This class is not being loaded due that I think that this folder is not in the path that Yii loads by default.

So the question, which is the best way to create a folder where I will keep my component and they will be loaded by Yii ?

I recommend to create this class in a separate folder and not touch the core yii folders as that may get overwritten. For example you could create it under app/components or common/components and extend any yii classes in here.

namespace common\components;

class MyDbManager extends yii\rbac\DbManager {

 //your code


You can then set this in your config like:

    'authManager' => [

        'class' => 'common\components\MyDbManager',


I think that at the end I realized how to do it.


[*]Create the folder

I created my folder under the root directory and is called components.

[*]Creating the namespace as the folder

The class that I created includes the namespace app\components on it.


namespace app\components;

use Yii;

use yii\db\Connection;

use yii\db\Query;

use yii\db\Expression;

use yii\base\InvalidCallException;

use yii\base\InvalidParamException;

use yii\di\Instance;

use yii\rbac\DbManager;

use yii\rbac\Assignment;


 * Description of MyDbManager


 * @author ctala


class MyDbManager extends DbManager {

[*]Calling the Class

Now, in the ocnfiguration file I can call the class without troubles.

        'authManager' => [

            'class' => '\app\components\MyDbManager',



I think that now I understand a little bit better namespaces. I will write a wiki with this information in case someone has the same trouble that I had.

Wait, what? Are you referring to the vendor/yiisoft/yii2 folder? You shouldn’t be touching anything inside the vendor folder! That is for composer to manage properly. (It’s dangerous because it “updates” by deleting/reinstalling the packages, meaning any changes you make will disappear.)

That’s exactly how I described it in my first post here. What did you do differently that made it not work the first time but then did work the second time? Perhaps you should put that in the wiki as well.