yii2 扩展和自定义(函数 , 组件 , 模块)
来源:互联网 发布:ubuntu安装py模块 编辑:程序博客网 时间:2024/05/29 12:28
自定义辅助函数
入口文件加载
目录下创建一个helpers目录下创建functions.php 文件
<?php
if (! function_exists('hello')) {
function hello(){
echo 'hello word';
}
}
if (! function_exists('hello')) {
function hello(){
echo 'hello word';
}
}
修改项目入口文件index.php
新增如下代码:
require(__DIR__ . '/../helpers/functions.php');
新增如下代码:
require(__DIR__ . '/../helpers/functions.php');
composer中设置加载(推荐)
在 composer.json 文件里面添加如下代码:
"autoload": {
"files": [
"common/components/functions.php"
]
},
添加完之后,在common/components下添加文件functions.php,项目根目录下执行 composer update
ok!
自定义component 组件
在app\components下新建NewComponent.php
namespace app\components;
use Yii;
use yii\base\Component;
use yii\base\InvalidConfigException;
class NewComponent extends Component
{
public function hello()
{
echo "hello world";
}
}
use Yii;
use yii\base\Component;
use yii\base\InvalidConfigException;
class NewComponent extends Component
{
public function hello()
{
echo "hello world";
}
}
main.php配置文件中
'components' => [
'testcomponent' => [
'class' => 'app\components\MyComponent',
],
]
'testcomponent' => [
'class' => 'app\components\MyComponent',
],
]
下面就可以愉快的使用 组件了是不是很简单 !
Yii::$app->testcomponent->hello();
自定义Modules 模块
以下参考yii2.0 权威指南
新建一个如下目录
forum/
Module.php 模块类文件
controllers/ 包含控制器类文件
DefaultController.php default 控制器类文件
models/ 包含模型类文件
views/ 包含控制器视图文件和布局文件
layouts/ 包含布局文件
default/ 包含DefaultController控制器视图文件
index.php index视图文件
Module.php 模块类文件
controllers/ 包含控制器类文件
DefaultController.php default 控制器类文件
models/ 包含模型类文件
views/ 包含控制器视图文件和布局文件
layouts/ 包含布局文件
default/ 包含DefaultController控制器视图文件
index.php index视图文件
Module.php 代码如下
namespace app\modules\forum;
class Module extends \yii\base\Module
{
public function init()
{
parent::init();
$this->params['foo'] = 'bar';
// ... 其他初始化代码 ...
}
}
class Module extends \yii\base\Module
{
public function init()
{
parent::init();
$this->params['foo'] = 'bar';
// ... 其他初始化代码 ...
}
}
如果 init() 方法包含很多初始化模块属性代码, 可将他们保存在配置 并在init()中使用以下代码加载:
public function init()
{
parent::init();
// 从config.php加载配置来初始化模块
\Yii::configure($this, require(__DIR__ . '/config.php'));
}
config.php配置文件可能包含以下内容,类似应用主体配置.
<?php
return [
'components' => [
// list of component configurations
],
'params' => [
// list of parameters
],
];
public function init()
{
parent::init();
// 从config.php加载配置来初始化模块
\Yii::configure($this, require(__DIR__ . '/config.php'));
}
config.php配置文件可能包含以下内容,类似应用主体配置.
<?php
return [
'components' => [
// list of component configurations
],
'params' => [
// list of parameters
],
];
使用模块
要在应用中使用模块,只需要将模块加入到应用主体配置的yii\base\Application::modules属性的列表中, 如下代码的应用主体配置 使用 forum 模块:
[
'modules' => [
'forum' => [
'class' => 'app\modules\forum\Module',
// ... 模块其他配置 ...
],
],
]
[
'modules' => [
'forum' => [
'class' => 'app\modules\forum\Module',
// ... 模块其他配置 ...
],
],
]
访问路由
forum/post/index 代表模块中 post 控制器的 index 操作
就是这么简单 !
阅读全文
0 0
- yii2 扩展和自定义(函数 , 组件 , 模块)
- Yii2 自定义组件
- yii2自定义组件
- yii2.0自定义组件
- YII2.0自定义组件
- yii2 添加 自定义 组件 custom component,以及模块 module 原理的详解剖析
- php扩展SeasLog应用于 yii2 组件
- php扩展SeasLog应用于 yii2 组件
- extjs6 基础组件扩展和自定义组件封装--01前言
- Yii2里自定义函数的保存位置和使用方法
- 游戏引擎扩展的两种方式--组件和模块
- Yii2 自定义class、全局函数
- [置顶] YII2 模块内自定义错误页
- yii2在模块内自定义错误
- Yii2 自定义class、自定义全局函数
- yii2自定义GridView和ActionColumn
- flex 3扩展自定义组件
- 扩展/自定义 Extjs.Toolbar 组件
- node.js学习笔记之HTTP---发送服务器端响应流
- git plumbing 更加底层命令解析-深入理解GIT
- TCP三次握手与挥手
- 如何在linux console中显示当前你在的branch?
- git项目实战常用workflow和命令
- yii2 扩展和自定义(函数 , 组件 , 模块)
- linux下从源代码安装git
- 使用git submodule管理一个需要多个分立开发或者第三方repo的项目
- 使用public key来做SSH authentication
- PHP项目中composer和Git的组合使用
- [LeetCode]1. Two Sum
- git subtree有效管理公共第三方lib
- B和B+树
- 如何将开源项目部分代码作为private放在github上?