简体   繁体   中英

Yii Autoloading Custom Components

I'm trying to get Yii to autoload a component that doesn't follow Yii conventions. The library in question is Stripe which I moved into the Components folder. I was able to get it to autoload the main Stripe file no problem by modifying my config like this:

'import'=>array(
    'application.models.*',
    'application.components.*',
    'application.components.stripe.*',
),

The directory structure looks like:

Components
----------
  |
  ----> Stripe
     | 
     ----> Stripe.php    (Class Name = "Stripe")
     ----> Customer.php  (Class Name = "Stripe_Customer")
     ----> Charge.php    (Class Name = "Stripe_Charge")
     ----> etc.

It has no problem recognizing the Stripe class, but can't find the Stripe_* named classes. Is there a way to get Yii to recognize this pattern or am I stuck with manual require statements in my controller? Would it work if I renamed all the files to be what their class name is?

Yes, renaming the file to the class name would help. Otherwise you have to require the classes by yourself.

You could also create your own autoloader and register it with: Yii::registerAutoloader(array("AutoloderClass", "methodName"), $append); .
First parameter is actually a php callback, i used class and method notation, can be function as well. This actually calls spl_autoload_register in order depending on second param - before or after Yii autoloader. Autoloader should be registered in index.php just before $app->run() .

Some sample autoloader (for zend framework) can be found here: http://www.yiiframework.com/extension/zendautoloader

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM