Jeremy Harris
Jeremy Harris

Reputation: 24579

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?

Upvotes: 2

Views: 3155

Answers (2)

user133408
user133408

Reputation:

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

Upvotes: 2

Tim
Tim

Reputation: 963

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

Upvotes: 2

Related Questions