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 关于#Mybatis#的问题:怎么办重复数据如下"KSDM":37,"KSMC":"呼吸内科门诊","ksdm":37,"ksmc":"呼吸内科门诊"xml文件填写
  • ¥40 servlet的web程序部署出错
  • ¥50 activiti 新建流程系列问题
  • ¥50 为什么我版本升级之后运行速度变慢5倍??
  • ¥15 如何在gazebo中加载机械臂和机械手
  • ¥15 纯运放实现隔离采样方案设计
  • ¥20 easyconnect无法连接后缀带.com的网站,一直显示找不到服务器ip地址,但是带有数字的网站又是可以连接上的,如何解决
  • ¥15 电脑开机过商标后就直接这样,求解各位
  • ¥15 mysql , 用自己创建的本地主机和用户名 登录不上
  • ¥15 关于#web项目#的问题,请各位专家解答!