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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条