zoukankan      html  css  js  c++  java
  • phalapi

    public为程序入口

    Demo和MyApp为具体的实现

    public为多入口

    demo和myapp都是入口,但是进入后即又为单入口

    list为接口文档,是自动解析程序里的注释自动生成的文档

    框架执行流程

    Public/demo-》Demo/Api-》Demo/Domain-》Demo/Model

    Api/User.php

    主要是接收传参,以及调用Domain/User.php里的方法(有点像实体类)

    <?php
    
    class Api_User extends PhalApi_Api {
    
        public function getRules() {
            return array(
                'getBaseInfo' => array(
                    'userId' => array('name' => 'user_id', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '用户ID'),
                ),
                'getMultiBaseInfo' => array(
                    'userIds' => array('name' => 'user_ids', 'type' => 'array', 'format' => 'explode', 'require' => true, 'desc' => '用户ID,多个以逗号分割'),
                ),
            );
        }
    
        /**
         * 获取用户基本信息
         * @desc 用于获取单个用户基本信息
         * @return int code 操作码,0表示成功, 1表示用户不存在
         * @return object info 用户信息对象
         * @return int info.id 用户ID
         * @return string info.name 用户名字
         * @return string info.note 用户来源
         * @return string msg 提示信息
         */
        public function getBaseInfo() {
            $rs = array('code' => 0, 'msg' => '', 'info' => array());
    
            $domain = new Domain_User();
            $info = $domain->getBaseInfo($this->userId);
    
            if (empty($info)) {
                DI()->logger->debug('user not found', $this->userId);
    
                $rs['code'] = 1;
                $rs['msg'] = T('user not exists');
                return $rs;
            }
    
            $rs['info'] = $info;
    
            return $rs;
        }
    
        /**
         * 批量获取用户基本信息
         * @desc 用于获取多个用户基本信息
         * @return int code 操作码,0表示成功
         * @return array list 用户列表
         * @return int list[].id 用户ID
         * @return string list[].name 用户名字
         * @return string list[].note 用户来源
         * @return string msg 提示信息
         */
        public function getMultiBaseInfo() {
            $rs = array('code' => 0, 'msg' => '', 'list' => array());
    
            $domain = new Domain_User();
            foreach ($this->userIds as $userId) {
                $rs['list'][] = $domain->getBaseInfo($userId);
            }
    
            return $rs;
        }
    }

    Domain/User.php

    主要是方法的抽象实现,不是具体实现,类似于一个服务,这样有利于方法重用

    <?php
    
    class Domain_User {
    
        public function getBaseInfo($userId) {
            $rs = array();
    
            $userId = intval($userId);
            if ($userId <= 0) {
                return $rs;
            }
    
            // 版本1:简单的获取
            $model = new Model_User();
            $rs = $model->getByUserId($userId);
    
            // 版本2:使用单点缓存/多级缓存 (应该移至Model层中)
            /**
            $model = new Model_User();
            $rs = $model->getByUserIdWithCache($userId);
            */
    
            // 版本3:缓存 + 代理
            /**
            $query = new PhalApi_ModelQuery();
            $query->id = $userId;
            $modelProxy = new ModelProxy_UserBaseInfo();
            $rs = $modelProxy->getData($query);
            */
    
            return $rs;
        }
    }

    Model/User.php

    方法的具体实现,最小粒度

    <?php
    
    class Model_User extends PhalApi_Model_NotORM {
    
        public function getByUserId($userId) {
            return $this->getORM()
                ->select('*')
                ->where('id = ?', $userId)
                ->fetch();
        }
    
        public function getByUserIdWithCache($userId) {
            $key = 'userbaseinfo_' . $userId;
            $rs = DI()->cache->get($key);
            if ($rs === NULL) {
                $rs = $this->getByUserId($userId);
                DI()->cache->set($key, $rs, 600);
            }
            return $rs;
        }
    
        /**
        protected function getTableName($id) {
            return 'user';
        }
        */
    }

    缓存方法

     public function getByUserIdWithCache($userId) {
            $key = 'userbaseinfo_' . $userId;
            $rs = DI()->cache->get($key);
            if ($rs === NULL) {
                $rs = $this->getByUserId($userId);
                DI()->cache->set($key, $rs, 600);
            }
            return $rs;
        }

    如果缓存不存在,执行sql查询

    否则直接返回缓存值

    一个很漂亮的接口文档

  • 相关阅读:
    【极角排序、扫描线】UVa 1606
    【计算几何】是时候知道这些函数了
    【技巧性(+递归运用)】UVa 1596
    【策略】UVa 11389
    【策略】UVa 1344
    夏季吃西瓜,会勤上厕所,会导致体内缺水,导致便秘,,会长豆豆
    Alt+Shift+R组合键,用来在一个java文件中批量的重命名变量。
    Pycharm-professional-2017.2.3破解安装
    通过反编译深入理解Java String及intern
    转:MyEclipse安装Eclipse Memory Analyzer插件,并进行错误文件分析流程
  • 原文地址:https://www.cnblogs.com/hellowzd/p/6264136.html
Copyright © 2011-2022 走看看