如何扩展 Yii 框架类以及放置文件的位置

编程入门 行业动态 更新时间:2024-10-20 13:30:10
本文介绍了如何扩展 Yii 框架类以及放置文件的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我想扩展一些内置框架类,但我有一些不清楚的地方:

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

我想我应该把我的类放在我的组件文件夹下,这是正确的地方吗?我应该将类命名为 Subfolder_ClassName 之类的文件,并将文件命名为 ClassName.php 之类的吗?我如何继承?默认包含路径不包含框架本身.我认为有一种内置的方法可以做到这一点,而无需更改包含路径/开始使用自动加载功能或简单地硬编码包含在我的代码中. I assume I should put my classes under my components folder, is this the right place? Should I name the classes like Subfolder_ClassName and the files like ClassName.php? 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.

对吗?

推荐答案

源文件的放置

将它们放在 /protected/components 下是很自然的选择.如果您以后开发更多可以跨项目重用的通用类,您可以考虑将它们放到一个单独的目录中.

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.

命名和目录结构

这取决于您的应用的规模.如果你没有很多组件(比如 20 个以下),恕我直言,你不需要任何目录结构,components 目录对所有组件都有好处.另一种实用的方法是将业务组件放在一个目录中,将 HTML 组件放在另一个目录中(例如 widgets).

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

包括父类

你的配置文件应该有一个这样的部分:

Your configuration file should have a section like this:

// autoloading model and component classes
'import'=>array(
    'application.models.activerecord.*',
    'application.models.forms.*',
    'applicationponents.*',
    'application.classes.*',
    // etc
),

这部分实际上导致 CModule::setImport 在您的应用程序实例上被调用以注册可以自动加载的类.如果你想扩展这些类,那么你不需要做任何事情.

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.

如果您的某些组件依赖于未按上述方式导入的 Yii 类,您将需要使用 Yiibase::import 在声明你的类之前.例如:

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
}

为路径注册别名

如果您想为导入路径创建自己的别名(例如,有一个路径 myappponents 可以在导入类时引用),您可以使用 Yiibase::setPathOfAlias.一个很好的地方是配置文件的最顶部:

If you want to create your own aliases for import paths (e.g. to have a path myappponents 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('myappponents','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...
       'myappponents.*',
       // ...other imports...
   ),
);

这篇关于如何扩展 Yii 框架类以及放置文件的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-23 19:00:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1047623.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:框架   位置   文件   Yii

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!