Žiadny popis https://www.travelzs.com

zjh 7e475cf0f4 新增获取字段方法 2 dní pred
_cli c916183ac0 修复相对路径问题 3 rokov pred
demo 9b91912fc7 update model 1 rok pred
src 7e475cf0f4 新增获取字段方法 2 dní pred
.DS_Store b4aada2966 update route 1 rok pred
.gitignore c916183ac0 修复相对路径问题 3 rokov pred
README.md d6755fa4f5 Add middleware 2 rokov pred
_cli.php 6998afa958 Fixed argv问题 2 rokov pred
composer.json 9272ddd97a Update: entity base新增to array 3 mesiacov pred

README.md

新版Qii

使用方法: 1、composer install

composer.json:

{
    "name": "zhujinhui/svideo",
    "description": "发布平台",
    "type": "project",
    "license": "MIT",
    "authors": [
        {
            "name": "朱金辉",
            "email": "jinhui.zhu@live.cn"
        }
    ],
    "config": {
        "secure-http": false,
        "vendor-dir": "../vendor"
    },
    "minimum-stability": "alpha",
    "repositories": [
        {
            "type": "vcs",
            "url": "https://code.istudy.wang/root/qii.git"
        },
        {"packagist": false}
    ],
    "require": {
        "jinhui.zhu/qii": "1.0.3"
    }
}

2、创建项目

通过命令行进入qii目录,并执行:php -q _cli.php create=yes workspace=../project cache=tmp useDB=1
或者拷贝demo目录文件到你的项目中
Command line usage:
php -q _cli.php 并根据提示完成网站的配置
程序将自动创建工作目录,并生成首页及配置相关文件。设置好Web网站目录,配置好Nginx或Apache开启.htaccess即可直接访问。
相关的配置文件见 configure/app.ini及configure/db.ini文件

3、推荐使用rewrite短链的方式运行代码

根目录指向到public目录,并设置rewrite规则
Nginx配置rewrite规则
```
location / {
    try_files $uri $uri/ =404;
    if (!-e $request_filename) {
        rewrite /(.*) /index.php;
    }
}
```
Apache配置配置,在网站目录中创建.htaccess并开启rewrite
```
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f

    RewriteCond $1 !^(index.php|images|robots.txt)
    RewriteRule (.*)$ index.php/$1 [L,QSA,PT]
```

4、框架的使用

1) 命令行运行程序, 推荐使用short模式
仅支持GET方法
    1. normal模式
    php -q index.php control=index/action=home/id=100
    php -q index.php control=plugins/action=index/page=2

    2. middle模式
    php -q index.php control/index/action/home/id/100
    php -q index.php control/plugins/action/index/page/2

    3. short模式
    php -q index.php index/home/100
    php -q index.php plugins/page/2.html
2) 自动加载类
    new controller\user(); === require("controller<?=DS?>user.php"); new controller\user();
    new model\user(); === require("model<?=DS?>user.php"); new model\user();
    new model\test\user(); === require("model<?=DS?>test<?=DS?>user.php"); new model\test\user();

3) Qii 基本功能:
    \Qii::getInstance(className, param1, param2, param3[,...]); === ($class = new className(param1, param2, param3[,...]));
    _loadClass(className)->method(); === $class->method();
    _library('Cyrpt') == new \Qii\Library\Crypt();
    _require(fileName); 如果指定的文件无法找到则会在get_include_path()和站点配置文件的[path]的目录中去搜索,直到搜索到一个则停止。
    _include(fileName); == include(fileName);
    _config($key, $value);保存到私有变量 $_global[$key]中, $value可以为数组,如果是数组的话会将数组合并到已经存在$key的数组中去。
    _config($key);获取$_global[$key][$index]的值
    \Qii::setError($condition, $code, $argvs); 检查$condition是否成立,成立就没有错,返回false,否则有错,返回true并将错误信息,详细代码错误$code详情见<?php echo \Qii::getPrivate('qii_sys_language');?>。
4) 多域名支持:
    开启多域名支持,不同域名可以访问不同目录中的controller,在app.ini中的common下添加以下内容,注意:hosts中的内容会覆盖网站中对应的配置
    hosts[0.domain] = test.xxx.wang
    hosts[0.path] = test

    hosts[1.domain] = admin.xxx.wang
    hosts[1.path] = admin
5) Module用法示例:
    第一步,创建一个user的model
    class user extends \Qii\Driver\Model
    {
        public function __construct()
        {
            parent::__construct();
        }
        public function userInfo($uid)
        {
            reutrn $this->db->where(['uid' => $uid])->selectRow('user');
            //或者
            return $this->db->getRow("SELECT * FROM user WHERE uid = '{$uid}'");
        }

        public function updateUserInfo($uid, $map)
        {
            return $this->db->updateObject('user', $map, array('uid' => $uid));
        }

        public function removeUser($uid)
        {
            return $this->db->exec("DELETE FROM user WHERE uid = ". intval($uid));
            //或
            return $this->db->where(['uid' => $uid])->deleteRows('user');
        }
    }
    第二步,使用user model
    namespace controler;

    class user extends \Qii\Base\Controller
    {
        public function __construct()
        {
            $userClass = new \model\user();
            $userInfo = $userClass->userInfo(10);

            或
            _loadClass("model\user")->userInfo(10);
        }
    }
6) ORM的使用示例:
    第一步,创建表对应的ORM模型
    /**
     * User ORM 模型
     * @author Zhu Jinhui 2015-02-13 11:26
     *
     */
    class User extends Tables
    {
        public function getTableName()//返回User对应的数据表
        {
            return 'istudy_user';
        }
        public function getRelationMap()//返回alias对应的字段名
        {
            return array('id' => 'uid', 'email' => 'email', 'nick' => 'nickname');
        }
        public function getValidateSaveFields()//保存数据需要验证的字段
        {
            return array('email', 'password');
        }
        public function getValidateRules()//验证规则
        {
            return array('email' => array('email' => true), 'password' => array('password' => true, 'length' => 32));
        }
        public function getInvalidMessage()//验证不通过返回的消息内容
        {
            return array();
        }
    }
    第二步,创建User Model:
    namespace model;
    class user extends \Qii\Driver\Model
    {
        public function __construct()
        {
            parent::__construct();
        }
        /**
         * 注册
         * @param String $email
         * @param String $password
         * @return Array
         */
        public function register($email, $password)
        {
            $data = array();
            if(!$email || !$password)
            {
                $data['code'] = 1;
                $data['error'] = array('result' => '参数不正确');
                return $data;
            }
            $user = new User();
            $user->email = $email;
            $user->password = md5($password);
            $user->active_code = substr(md5(uniqid(rand(), TRUE)), -6);
            $user->add_time = time();
            $user->update_time = time();

            $user->setPrivateKey('email');
            $isExists = $user->isExits();

            if($isExists)
            {
                $data['code'] = 10001;
                $data['data'] = $isExists;
            }
            else
            {
               $data['code'] = 0;
               $data['uid'] = $user->execSave();
               if($user->getTablesError())
               {
                   $data['error'] = $user->getTablesError();
               }
            }
            return $data;
        }

        public function login($email, $password)
        {
            $data = array();
            if(!$email || !$password)
            {
                return $data;
            }
            $user = new User();
            $user->email = $email;
            //$user->password = md5($password);
            $userInfo = $user->isExits();
            if($userInfo['uid'])
            {
                if($userInfo['password'] != md5($password))
                {
                    $data['code'] = 1;
                    $data['msg'] = '密码不正确';
                }
                else
                {
                   $data['code'] = 0;
                   $cookie['uid'] = $userInfo['uid'];
                   $cookie['email'] = $userInfo['email'];
                   $data['cookie'] = $cookie;
                } 
            }
            return $data;
        }
    }
    第三步,使用model\user:

    class index_controller extends \Qii\Controller\Abstract
    {
        public function __construct()
        {
            $user = new model\user();
            $status = $user->login('email@test.com', '119328118');
            if($status['code'] === 0) echo '登录成功';
        }
    }
Model新玩法:
	class base extends \Qii\Driver\Model {
		public function __construct()
		{
			parent::__construct();
		}
		public function demo()
		{
			$this->db->fields("*")->join(array("leftJoin", array('table' => 'ad_site', 'alias' => 'b', 'on' => 'a.uid = b.uid')))->where(array('a.create_at:greater' => 1))->limit(10)->groupBy(array('a.uid', 'a.create_at'))->orderBy(array('a.uid' => 'desc'))->selectAll('user a');
			$this->db->where(array('uid' => 1))->set(array('create_at:plus' => time(), 'status' => 1))->update('user');
			$this->db->updateObject('user', array('create_at:plus' => time(), 'status' => 1), array('uid' => 1));
			$this->db->where(array('uid:greater' => 1))->delete('user');
			$this->db->deleteObject('user', array('uid' => 1));
			$this->db->where(array('uid:greater' => 1))->like("email like '%test@test.com%'")->selectAll('user');
			$this->db->where(array('uid:greater' => 1))->where("or")->like(array('email' => 'test@testcom'))->selectAll('user');
			$this->db->where(array('uid:greater' => 1, 'or', 'email:like' => 'test@test.com'))->selectAll('user');
			$this->db->where(array('uid:greater' => 1))->where("OR")->like(array('email' => 'test@test.com'))->selectAll('user');
			$this->db->where(array('name' => 'antsnet'))->exclude(array('email' => 'test@test.com', 'status' => 1))->selectAll('user');
			$this->db->where(array('name' => 'antsnet'))->or(array('email' => 'test@test.com', 'status' => 1))->selectAll('user');
			$this->db->or(array('email' => 'test@test.com', 'status' => 1))->selectAll('user');
			$this->db->join(array("leftJoin", array("table" => 'table', 'alias' => 'a', 'on' => 'a.id=b.id')));
			$this->db->join(" LEFT JOIN table c on c.id=a.id")->selectAll('use a');
			$this->db->where(array('email:unequal' => 'test@test.com'))->in(array('uid:in' => array('1,2,3'), 'status' => array(1)))->selectAll('user');
			$this->db->where(array(array('email:unequal' => 'test@test.com'), array('uid:in' => array('1,2,3'), 'status:in' => array(1))))->selectAll('user');
		}
	}
7) View的支持
    view支持smarty及php
    class index extends \Qii\Controller\Abstract
    {
        public function __construct()
        {
            $this->enableView();//默认是app.ini中的view[engine],你可以在此处选择是用include或者require,只要将参数默认传给enableView即可
            $this->view->display('tpl'); //$this->view即为使用的模板引擎
        }
    }
8) Controller的使用
class test extends \Qii\Controller\Abstract
{
    //为了避免Controller逻辑过于复杂,可以将Action拆到单独的文件
    //当在调用dummy方法的时候会自动执行actions/dummy_action.php中的execute方法
    public $actions = array(
            "dummy" => "actions/dummy_action.php",
    );
    public function __construct()
    {
        parent::__construct();
    }
}
9) Cache支持
Controller中使用Cache
class cache extends Controller
{
    public function __construct()
    {
        parent::__construct();
    }
    public function cacheTest()
    {
        $cache_id = 'cache_test';
        //文件缓存
        $this->setCache('file', array('path' => 'tmp'));
        //Memcache缓存
        $this->setCache('memcache', array('servers' => array( array('host'=>'127.0.0.1','port'=>11211) ) ,'life_time'=>600));
        //xcache
        $this->setCache('xcache', array('life_time'=>600));
        //缓存内容
        $this->cache($cache_id, 'cache内容');
        //redis缓存
        $this->setCache('redis', array('servers' => array('127.0.0.1.6379')));
        $this->cache($cache_id, array('cache' => 'cache 内容'));
        //获取缓存内容
        $this->getCache($cache_id);
        //移除缓存
        $this->cache->remove($cache_id);
    }
}

Model中使用
class cache extends Model
{
    public function __construct()
    {
        parent::__construct();
    }
    public function cacheTest()
    {
        $cache_id = 'cache_test';
        //文件缓存
        $this->setCache('file', array('path' => 'tmp'));
        //Memcache缓存
        $this->setCache('memcache', array('servers' => array( array('host'=>'127.0.0.1','port'=>11211) ) ,'life_time'=>600));
        //xcache
        $this->setCache('xcache', array('life_time'=>600));
        //缓存内容
        $this->cache($cache_id, 'cache内容');
        //获取缓存内容
        $this->getCache($cache_id);
        //移除缓存
        $this->cache->remove($cache_id);
    }
}
Middleware使用:
$app = \Qii::getInstance();
全局middleware,所有请求过来以后都会先执行
$app->setGlobalMiddle('\middleware\site');
route路由middleware,见demo中的使用
$app->run();
  • 更多好的方法见方法提供的文件示例或demo文件。