开发者

How to extend Yii framework classes and where to place the files

开发者 https://www.devze.com 2023-04-06 00:28 出处:网络
I want to extend some built-in framework classes, but I there are some points I \'m not clear about: I assume I should put my classes under my components folder, is this the right place?

I want to extend some built-in framework classes, but I there are some points I 'm not clear about:

  1. I assume I should put my classes under my components folder, is this the right place?
  2. Should I name the classes l开发者_开发知识库ike Subfolder_ClassName and the files like ClassName.php?
  3. How do I inherit? The default include paths does not include the framework itself. I assume there is a built in way to do it without me changing the include path/start playing with the autoload function or Simply hard code an include in my code.

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


If you want to create a child class of a class in the yii-Framework, yeah you can place your class definition file within project/protected/components. Depends on the best practices you want in place (i.e. will you be creating dozens of custom classes vs. just one or two that extend FW classes) but placing your file directly in components should be fine in terms of organization.

To inherit you can just create your file along these lines:

class MyNewClass extends CFrameworkClass
{
    //All your stuff

} 

Note that by convention the yii framwork classes have "C" in front of them (i.e. CController), I'm not adding the "C" there (CFramwork..) for any other reason than keeping with the convention.

0

精彩评论

暂无评论...
验证码 换一张
取 消