Yii Framework Forum: Including My Own Folder Of Components In The Namespace - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Including My Own Folder Of Components In The Namespace Which is the best practice ?

#1 User is offline   CTala 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 94
  • Joined: 17-August 11
  • Location:Chile

Posted 30 April 2014 - 09:04 AM

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 ?
--
MSc. Cristian Tala Sánchez
0

#2 User is offline   alex-ks 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 56
  • Joined: 29-August 11
  • Location:Minsk, Belarus

Posted 30 April 2014 - 09:33 AM

View PostCTala, on 30 April 2014 - 09:04 AM, said:

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

#3 User is offline   CTala 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 94
  • Joined: 17-August 11
  • Location:Chile

Posted 30 April 2014 - 11:40 AM

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.
--
MSc. Cristian Tala Sánchez
0

#4 User is offline   skworden 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 165
  • Joined: 14-June 12
  • Location:Virginia, USA

Posted 02 May 2014 - 02:42 PM

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

#5 User is offline   amnah 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 105
  • Joined: 23-February 13

Posted 02 May 2014 - 04:26 PM

View PostCTala, on 30 April 2014 - 11:40 AM, said:

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.


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

For example,

// app\libraries\MyLibrary.php
<?php

namespace app\libraries;

class MyLibrary{ }


Then call it with the following:

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

0

#6 User is offline   CTala 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 94
  • Joined: 17-August 11
  • Location:Chile

Posted 03 May 2014 - 08:21 AM

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.
--
MSc. Cristian Tala Sánchez
0

#7 User is offline   Kartik V 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 612
  • Joined: 29-August 12

Posted 03 May 2014 - 08:26 AM

View PostCTala, on 03 May 2014 - 08:21 AM, said:

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

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

#8 User is offline   CTala 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 94
  • Joined: 17-August 11
  • Location:Chile

Posted 06 May 2014 - 11:27 AM

Look,

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 ?
--
MSc. Cristian Tala Sánchez
0

#9 User is offline   Kartik V 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 612
  • Joined: 29-August 12

Posted 06 May 2014 - 11:35 AM

View PostCTala, on 06 May 2014 - 11:27 AM, said:

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:

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',
    ],

1

#10 User is offline   CTala 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 94
  • Joined: 17-August 11
  • Location:Chile

Posted 06 May 2014 - 11:37 AM

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.

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

    
            '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.
--
MSc. Cristian Tala Sánchez
1

#11 User is offline   amnah 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 105
  • Joined: 23-February 13

Posted 06 May 2014 - 12:57 PM

View PostCTala, on 06 May 2014 - 11:27 AM, said:

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:



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

View PostCTala, on 06 May 2014 - 11:37 AM, said:


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.



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

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users