I want to extend some built-in framework classes, but I there are some points I 'm not clear about:
Subfolder_ClassName
and the files like ClassName.php
?Right?
Placement of source files
Putting them under /protected/components
is a natural choice. If you later develop more generic classes that can be reused across projects, you can think about putting those into a separate directory.
Naming and directory structure
This will depend on the scale of your app. If you don't have lots of components (say under 20) then IMHO you don't need any directory structure, the components
directory is good for all of them. Another practical approach is to put your business components into one directory and your HTML components into another (e.g. widgets
).
Including parent classes
Your configuration file should have a section like this:
// autoloading model and component classes
'import'=>array(
'application.models.activerecord.*',
'application.models.forms.*',
'application.components.*',
'application.classes.*',
// etc
),
This section actually results in CModule::setImport
being called on your application instance to register classes that can be autoloaded. If you want to extend these classes then you don't need to do anything.
If some of your components depend on Yii classes that don't get imported as above, you will need to explicitly import them using Yiibase::import
before declaring your class. For example:
Yii::import('zii.widgets.jui.CJuiSlider');
class MySlider extends CJuiSlider {
// class contents here
}
Registering aliases for paths
If you want to create your own aliases for import paths (e.g. to have a path myapp.components
you can reference when importing classes), you can do this using Yiibase::setPathOfAlias
. A good place for this is the very top of your configuration file:
<?php
Yii::setPathOfAlias('myapp.components','components/my/');
// This is the main Web application configuration. Any writable
// CWebApplication properties can be configured here.
return array(
// your whole app configuration is here, and you can now do this:
'import'=>array(
// ...other imports...
'myapp.components.*',
// ...other imports...
),
);