example.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. <html xmlns="http://www.w3.org/1999/html">
  2. <head>
  3. <title>使用方法 - Qii Framework</title>
  4. <meta name="keywords" content="Qii框架,PHP开发框架">
  5. <meta name="description" content="Qii 框架,以最简单的方式创建你的Web应用。" />
  6. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  7. <meta http-equiv="content-type" content="text/html;charset=utf-8">
  8. <?php include(Qii_DIR . '/view/style.php'); ?>
  9. </head>
  10. <body>
  11. <h1>使用方法:</h1>
  12. <ul>
  13. <li><a href="#createProject">创建项目</a></li>
  14. <li><a href="#env">设置环境</a></li>
  15. <li><a href="#useFramework">框架的使用</a>
  16. <ul>
  17. <li><a href="#useCommandLine">1)命令行运行程序</a></li>
  18. <li><a href="#autoload">2) 自动加载类</a></li>
  19. <li><a href="#basicMethod">3) Qii 基本功能:</a></li>
  20. <li><a href="#supportMultDomain"> 4) 多域名支持:</a></li>
  21. <li><a href="#modle"> 5) 数据库使用示例:</a></li>
  22. <li><a href="#view"> 6) View的支持</a></li>
  23. <li><a href="#controller"> 7) Controller的使用</a></li>
  24. <li><a href="#cache"> 8) Cache支持</a></li>
  25. </ul>
  26. </li>
  27. <li><a href="#download">下载</a></li>
  28. </ul>
  29. <pre>
  30. <a id="createProject"> 1、创建项目</a>
  31. <code>
  32. 通过命令行进入当前目录,并执行:php -q _cli.php create=yes workspace=../project cache=tmp useDB=1
  33. Command line usage:
  34. >php -q _cli.php create=yes workspace=../project cache=tmp useDB=1
  35. * create: is auto create default:yes;
  36. * workspace: workspace
  37. * cache : cache dir
  38. * useDB : use db or not
  39. 程序将自动创建工作目录,并生成首页及配置相关文件。设置好Web网站目录,开启.htaccess即可直接访问。
  40. 相关的配置文件见 configure/app.ini及configure/db.ini文件
  41. </code>
  42. <a id="env">2、设置环境</a>
  43. <code>
  44. 1、安装服务器软件(详情google或百度)
  45. 2、设置服务器的Rewrite规则:
  46. Apache:
  47. RewriteEngine On
  48. RewriteCond %{REQUEST_FILENAME} !-d
  49. RewriteCond %{REQUEST_FILENAME} !-f
  50. RewriteCond $1 !^(index.php|images|robots.txt)
  51. RewriteRule (.*)$ index.php/$1 [L,QSA,PT]
  52. RewriteRule ^(.*)\.phar$ - [F]
  53. RewriteRule ^(.*)\.ini$ - [F]
  54. Nginx:
  55. if (!-f $request_filename){
  56. rewrite (.*) /index.php;
  57. }
  58. location ~* /.(ini|phar){
  59. deny all;
  60. }
  61. </code>
  62. <a id="useFramework">3、框架的使用</a>
  63. <code>
  64. <a id="useCommandLine"> 1) 命令行运行程序</a>
  65. <code>
  66. 仅支持GET方法
  67. 1. normal模式
  68. php -q index.php controller=index/action=home/id=100
  69. php -q index.php controller=plugins/action=index/page=2
  70. 2. middle模式
  71. php -q index.php controller/index/action/home/id/100
  72. php -q index.php controller/plugins/action/index/page/2
  73. 3. short模式
  74. php -q index.php index/home/100
  75. php -q index.php plugins/page/2.html
  76. </code>
  77. <a id="autoload">2) 自动加载类</a>
  78. <code>
  79. new \controller\user(); === require("controller<?= DS ?>user.php"); new \controller\user();
  80. new \model\user(); === require("model<?= DS ?>user.php"); new \model\user();
  81. new \model\test\user(); === require("model<?= DS ?>test<?= DS ?>user.php"); new \model\test\user();
  82. </code>
  83. <a id="basicMethod">3) Qii 基本功能:</a>
  84. <code>
  85. Qii\Instance::Instance(className, param1, param2, param3[,...]); === ($class = new className(param1, param2,
  86. param3[,...]));
  87. Qii\Load::load(className)->method(); === $class->method();
  88. Qii\Import::requires(fileName); 如果指定的文件无法找到则会在get_include_path()和站点配置文件的[path]的目录中去搜索,直到搜索到一个则停止。
  89. Qii\Import::includes(fileName); == include(fileName);
  90. Qii::setPrivate($key, $value);保存到私有变量 $_global[$key]中, $value可以为数组,如果是数组的话会将数组合并到已经存在$key的数组中去。
  91. Qii::getPrivate($key, $index);获取$_global[$key][$index]的值
  92. Qii::setError($condition, $code, $argv1, $argv2, ...);
  93. 检查$condition是否成立,成立就没有错,返回false,否则有错,返回true并将错误信息,详细代码错误$code详情见<?php echo Qii::getPrivate('qii_sys_language'); ?>
  94. </code>
  95. <a id="supportMultDomain"> 4) 多域名支持:</a>
  96. <code>
  97. 开启多域名支持,不同域名可以访问不同目录中的controller,在app.ini中的common下添加以下内容,注意:hosts中的内容会覆盖网站中对应的配置
  98. hosts[0.domain] = test.xxx.wang
  99. hosts[0.ext] = test
  100. hosts[1.domain] = admin.xxx.wang
  101. hosts[1.ext] = admin
  102. </code>
  103. <a id="modle"> 5) 数据库使用示例:</a>
  104. <code>
  105. 1、创建配置文件:configure/istudy.istudy_user.config.php,你可以设置好数据库配置文件或者在这里<a
  106. href="<?= $this->_request->getFullUrl('main') . '?url=' . urlencode($this->_request->getFullUrl('database/creator')); ?>"><b>生成</b></a>配置文件并下载保存到本地目录。
  107. <?php highlight_file('configure/istudy.istudy_user.config.php') ?>
  108. namespace Model;
  109. use \Qii\Model;
  110. class user extends Model
  111. {
  112. public function __construct()
  113. {
  114. parent::__construct();
  115. }
  116. public function saveUserInfo()
  117. {
  118. $user = (new \Qii\Driver\Easy())->_initialize();
  119. //设置数据表的主键,保存的时候会自动验证指定数据是否已经存在
  120. $user->setPrivateKey(array('email'));
  121. //设置规则
  122. $user->setRules(new \Qii\Driver\Rules(\Qii\Import::includes('configure/istudy.istudy_user.config.php')));
  123. $user->nickname = 'antsnet';
  124. $user->sex = 1;
  125. $user->email = 'antsnet4@163.com';
  126. $user->password = 'A123456a';
  127. $user->add_time = time();
  128. $user->update_time = time();
  129. $user->status = 0;
  130. //此处返回response对象
  131. $response = $user->_save();
  132. if($response->isError())
  133. {
  134. return $response->getErrors();
  135. }
  136. return array('uid' => $response->getResult('_result'));
  137. }
  138. /**
  139. * 获取用户信息
  140. */
  141. public function userInfo($email)
  142. {
  143. return $this->db->getRow('SELECT * FROM istudy_user WHERE email = "'.$this->db->setQuote($email).'"');
  144. //或者
  145. return $this->db->where(array('email' => $email))->selectOne('istudy_user');
  146. //或者使用以下代码
  147. $user = (new \Qii\Driver\Easy('istudy_user'))->_initialize();
  148. //设置规则
  149. $user->setRules(new \Qii\Driver\Rules(\Qii\Import::includes('configure/istudy.istudy_user.config.php')));
  150. //操作的时候会根据规则自动验证对应字段是否符合指定规则,不符合就不做查询操作
  151. $response = $user->_getRowByEmail('antsnet@163.com');
  152. if($response->isError())
  153. {
  154. return array('users' => array(), 'error' => $user->getErrors());
  155. }
  156. return array('users' => $response->getResult('_result'));
  157. }
  158. /**
  159. * 登录
  160. */
  161. public function login($email, $password)
  162. {
  163. $user = (new \Qii\Driver\Easy('istudy_user'))->_initialize();
  164. //设置规则
  165. $user->setRules(new \Qii\Driver\Rules(\Qii\Import::includes('configure/istudy.istudy_user.config.php')));
  166. $user->setPrivateKey(array('email'));
  167. $user->email = $email;
  168. $response = $user->_exist();
  169. if($response->isError())
  170. {
  171. return array('login' => false, 'error' => $response->getErrors());
  172. }
  173. $data = $response->getResult();
  174. if($data['password'] != md5($password))
  175. {
  176. return array('login' => false, 'error' => 'invalid password');
  177. }
  178. return array('login' => true, 'res' => $data);
  179. }
  180. public function update($email, $password)
  181. {
  182. $user = (new \Qii\Driver\Easy('istudy_user'))->_initialize();
  183. //设置规则
  184. $user->setRules(new \Qii\Driver\Rules(\Qii\Import::includes('configure/istudy.istudy_user.config.php')));
  185. $user->setPrivateKey(array('email'));
  186. $user->email = $email;
  187. $user->func('md5', 'password', $password);
  188. $response = $user->_update();
  189. if($response->isError())
  190. {
  191. return array('update' => false, 'msg' => $response->getErrors());
  192. }
  193. return array('update' => true);
  194. }
  195. public function remove($email)
  196. {
  197. $user = (new \Qii\Driver\Easy('istudy_user'))->_initialize();
  198. //设置规则
  199. $user->setRules(new \Qii\Driver\Rules(\Qii\Import::includes('configure/istudy.istudy_user.config.php')));
  200. $user->setPrivateKey(array('email'));
  201. $user->email = $email;
  202. $response = $user->_remove();
  203. if($response->isError())
  204. {
  205. return array('remove' => false, 'msg' => $response->getErrors());
  206. }
  207. return array('remove' => true);
  208. }
  209. }
  210. 使用方法:
  211. namespace controller;
  212. use \Qii\Controller_Abstract;
  213. class test extends Controller_Abstract
  214. {
  215. //启用模板引擎支持
  216. protected $enableView = true;
  217. //启用数据库支持
  218. protected $enableModel = true;
  219. //修改默认模板引擎
  220. protected $viewType = array('engine' => 'include');
  221. public function __construct()
  222. {
  223. parent::__construct();
  224. }
  225. public function indexAction()
  226. {
  227. $this->_model->getRow('SELECT * FROM user WHERE email = "antsnet@163.com"');
  228. print_r($this->_load->model('user')->saveUserInfo());
  229. print_r($this->_load->model('user')->userInfo('antsnet@163.com'));
  230. print_r($this->_load->model('user')->login('antsnet@163.com', 'A123456a'));
  231. print_r($this->_load->model('user')->update('antsnet@163.com', 'A123456a'));
  232. print_r($this->_load->model('user')->remove('antsnet@163.com'));
  233. }
  234. }
  235. </code>
  236. <a id="view"> 6) View的支持</a>
  237. <code>
  238. view支持smarty及php
  239. use \Qii\Controller_Abstract;
  240. class index extends Controller_Abstract
  241. {
  242. //启用view,在调用parent::__construct()时自动初始化view
  243. protected $enableView = true;
  244. //启用database在调用parent::__construct()时自动初始化database
  245. protected $enableModel = true;
  246. //修改默认模板引擎
  247. protected $viewType = array('engine' => 'include');
  248. public function __construct()
  249. {
  250. parent::__construct();//默认是app.ini中的view[engine],你可以在此处选择是用include或者require,只要将参数默认传给enableView即可
  251. $this->_view->display('tpl'); //$this->view即为使用的模板引擎
  252. }
  253. }
  254. </code>
  255. <a id="controller"> 7) Controller的使用</a>
  256. <code>
  257. namespace controller;
  258. use \Qii\Controller_Abstract;
  259. class test extends Controller_Abstract
  260. {
  261. //为了避免Controller逻辑过于复杂,可以将Action拆到单独的文件
  262. //当在调用dummy方法的时候会自动执行actions\dummy中的execute方法
  263. public $actions = array(
  264. "dummy" => "actions\dummy",
  265. );
  266. public function __construct()
  267. {
  268. parent::__construct();
  269. }
  270. public function indexAction()
  271. {
  272. //转发到\controller\test的indexAction方法上
  273. $this->dispatch('test', 'index');
  274. //执行结束后再转发
  275. $this->forward('test', 'index');
  276. }
  277. //在执行parent::__construct()的时候会自动调用,如果return false即直接退出程序
  278. protected function beforeRun()
  279. {
  280. return true;
  281. }
  282. /**
  283. * 执行完dispatch后调用
  284. */
  285. protected function afterRun()
  286. {
  287. }
  288. }
  289. </code>
  290. <a id="cache"> 8) Cache支持</a>
  291. <code>
  292. Controller中使用Cache
  293. namespace controller;
  294. use \Qii\Controller_Abstract;
  295. class cache extends Controller_Abstract
  296. {
  297. public function __construct()
  298. {
  299. parent::__construct();
  300. }
  301. public function cacheTest()
  302. {
  303. $cache_id = 'cache_test';
  304. //文件缓存
  305. $this->setCache('file', array('path' => 'tmp'));
  306. //Memcache缓存
  307. $this->setCache('memcache', array('servers' => array( array('host'=>'127.0.0.1','port'=>11211) )
  308. ,'life_time'=>600));
  309. //xcache
  310. $this->setCache('xcache', array('life_time'=>600));
  311. //缓存内容
  312. $this->_cache->set($cache_id, 'cache内容');
  313. //redis缓存
  314. $this->setCache('redis', array('servers' => array('127.0.0.1.6379')));
  315. $this->_cache->set($cache_id, array('cache' => 'cache 内容'));
  316. //获取缓存内容
  317. $this->_cache->get($cache_id);
  318. //移除缓存
  319. $this->_cache->remove($cache_id);
  320. }
  321. }
  322. Model中使用
  323. namespace Model;
  324. use \Qii\Model;
  325. class cache extends Model
  326. {
  327. public function __construct()
  328. {
  329. parent::__construct();
  330. }
  331. public function cacheTest()
  332. {
  333. $cache_id = 'cache_test';
  334. //文件缓存
  335. $this->setCache('file', array('path' => 'tmp'));
  336. //Memcache缓存
  337. $this->setCache('memcache', array('servers' => array( array('host'=>'127.0.0.1','port'=>11211) )
  338. ,'life_time'=>600));
  339. //xcache
  340. $this->setCache('xcache', array('life_time'=>600));
  341. //缓存内容
  342. $this->_cache->set($cache_id, 'cache内容');
  343. //获取缓存内容
  344. $this->_cache->get($cache_id);
  345. //移除缓存
  346. $this->cache->remove($cache_id);
  347. }
  348. }
  349. </code>
  350. </code>
  351. <a id="download">下载</a>
  352. <code>
  353. 测试阶段暂时不提供下载
  354. </code>
  355. </pre>
  356. <?php include(Qii_DIR . '/view/footer.php'); ?>
  357. </body>
  358. </html>