rockstardev
rockstardev

Reputation: 13527

Where to place "common" classes in YII Framework structure?

I need to create a few classes and would like some help on where this would go in the YII Framework. I know if I create a Model, it must go in the "models" directory. And by the same logic I know where "views", "controllers" etc would go. However, where would the following be placed in my web application:

  1. A class that contains a variety of "number" functions such as currency conversion, metric conversions etc?
  2. A class that interacts with a REST API? (It interacts with the database)

Any tips?

Upvotes: 3

Views: 3845

Answers (3)

Brett Gregson
Brett Gregson

Reputation: 5913

Usually you can use any PHP class in within Yii. You can place it in the models folder (alongside the Yii generated models) and access them directly like so:

$myclass = new MyClass;
$myclass->methodname;

Alternatively (or if you run into any issues), you have can place it anywhere in your directory structure and include it in the main index.php (in the root) like so:

$myclass = dirname(__FILE__).'/myclass.php'; 
require_once($myclass);

Upvotes: 1

Dhaval
Dhaval

Reputation: 901

To get started with adding custom classeses on YII you can check below link.

http://www.yiiframework.com/wiki/165/understanding-autoloading-helper-classes-and-helper-functions/

Hope it'll help you to start.

Upvotes: 2

ragklaat
ragklaat

Reputation: 946

You can find an example here, it is pretty detailed in my opinion:

The directory structure of the Yii project site

Upvotes: 1

Related Questions