yii自定义组件

1.我们在backend文件下新建components目录作为自定义组件的文件
2.然后再文件夹下新建Sms.php文件,文件中的代码为

<?php
namespace backend\components;
use yii\base\Component;

class Sms extends Component {
    public function send($tel) {
        echo '给'.$tel.'成功发送了短信';
    }
}

3.配置文件中配置

'sms123' => [
            'class' => 'backend\components\Sms'
        ]

具体的代码如下,不用复制全部的代码,复制配置,放到自己的配置文件中,我们这里是配置在文件main-local.php中

<?php

$config = [
    'components' => [
        'request' => [
            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
            'cookieValidationKey' => 'BbrwMnBSkmMSoFKXSxr9a8DKal4jHaug',
        ],
        'view' => [
            // 'theme' => [
            //     // 'basePath' => '@backend/web/duanwu',
            //     'pathMap' => [
            //         '@backend/template' => [
            //             '@backend/template/chunjie',
            //             '@backend/template/duanwu'
            //         ]
            //     ]
            // ],
            // 'defaultExtension' => 'html'
        ],
        'i18n' => [
            'translations' => [
                'app*' => [
                    'class' => 'yii\i18n\PhpMessageSource',
                    'basePath' => '@backend/messages',
                    'fileMap' => [
                        'app' => 'app.php',
                        'app/error' => 'error.php',
                    ],
                ],
            ],
        ],
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'enableStrictParsing' => false,
        ],
        // 'errorHandler' => [
        //     'errorAction' => 'test/index17'
        // ]
        'sms123' => [
            'class' => 'backend\components\Sms'
        ]
    ],
    // 'controllerNamespace' => 'backend\control',
    'defaultRoute' => 'test',
    'viewPath' => '@backend/template',
    // 'layoutPath' => '@backend/template/public'
    // 'layout' => false
    // 'layout' => 'common'
    'language' => 'zh-CN',
    'modules' => [
        'shop' => [
            'class' => 'backend\modules\shop\Module',
        ]
    ]    
];

if (!YII_ENV_TEST) {
    // configuration adjustments for 'dev' environment
    $config['bootstrap'][] = 'debug';
    $config['modules']['debug'] = [
        'class' => 'yii\debug\Module',
    ];

    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = [
        'class' => 'yii\gii\Module',
    ];
}

return $config;

4.使用自定义组件
咋控制器中使用自定义组件

public function actionIndex18() {
        echo "<pre>";
        // print_r(Yii::$app->view);
        // print_r(Yii::$app->get('view'));
        // print_r(Yii::$app->getView());
        // print_r(Yii::$app->get('db'));
        Yii::$app->sms123->send('13900000000');
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

原克技术

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值