dsj8086 2019-01-19 17:10
浏览 14
已采纳

如何在YII2中逐步使用外部类[关闭]

So i have a class i have used couple of times for other non YII projects but now i want to use the same class in a YII2 project. I have done some searches but i kind of got stuck along the way. Below is what i have done so far:

I created a folder called "utility" in the vendor directory, the utility folder contains my class named "AT_Response.class.php". So my question is how do i include or call and use this class in my model or controller.

I have checked some links like :

https://www.yiiframework.com/doc/guide/2.0/en/tutorial-yii-integration

https://forum.yiiframework.com/t/not-understanding-how-to-use-external-php-library/79679

Class Code:

<?php

class AT_Response {

    static private $response = array
        (
        '9999' => array('description' => "Unexpected Response", 'definite' => true, 'status' => "Indeterminate"),
        '00' => array('description' => "Success", 'definite' => true, 'status' => "Success"),
        'NNC_AUTH_01' => array('description' => /*"Status unknown, please wait for settlement report"*/"System Error", 'definite' => true, 'status' => "Failure"),
        'NNC_VTU_01' => array('description' => "Ttimed out", 'definite' => false, 'status' => "Indeterminate"),
        'NNC_VTU_02' => array('description' => "Exceeded max number of requests for Phone number per time period", 'definite' => true, 'status' => "Failure"),
        'NNC_VTU_03' => array('description' => "Invalid target MSISDN supplied", 'definite' => true, 'status' => "Failure"),
        '-1' => array('description' => "Not successful", 'definite' => false, 'status' => "Failure"),
    );

    static function getResponseByCode($respCode) {
        if (isset(self::$response[$respCode]))
            return self::$response[$respCode];

        //else
        return self::$response['9999'];
    }

}

Thanks

  • 写回答

1条回答 默认 最新

  • doufunuo4787 2019-01-19 21:12
    关注

    With a few changes you can use any custom class as a helper component. You need to use namespace and use statement for the existing class you have, see the below

    <?php
    namespace app\components;
    
    class Response
    {
    
        /**
         * @var array
         */
        private static $response = array
            (
            '9999' => array('description' => "Unexpected Response", 'definite' => true, 'status' => "Indeterminate"),
            '00' => array('description' => "Success", 'definite' => true, 'status' => "Success"),
            'NNC_AUTH_01' => array('description' => /*"Status unknown, please wait for settlement report"*/"System Error", 'definite' => true, 'status' => "Failure"),
            'NNC_VTU_01' => array('description' => "Ttimed out", 'definite' => false, 'status' => "Indeterminate"),
            'NNC_VTU_02' => array('description' => "Exceeded max number of requests for Phone number per time period", 'definite' => true, 'status' => "Failure"),
            'NNC_VTU_03' => array('description' => "Invalid target MSISDN supplied", 'definite' => true, 'status' => "Failure"),
            '-1' => array('description' => "Not successful", 'definite' => false, 'status' => "Failure")
        );
    
        /**
         * @param $respCode
         */
        public static function getResponseByCode($respCode)
        {
            if (isset(self::$response[$respCode])) {
                return self::$response[$respCode];
            }
    
            return self::$response['9999'];
        }
    
    }
    

    Save the Above class in a file called Response.php in app\components folder if using basic-app or common\components if you are using advanced-app but dont forget to change the namespace in the code.

    You can then call the function getResponseByCode() like app\components\Response::getResponseByCode($responseCode) or common\components\Response::getResponseByCode($responseCode)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助