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.
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.
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:
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
}
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.
<?php
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.
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.