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. 。
  95. </code>
  96. <a id="supportMultDomain"> 4) 多域名支持:</a>
  97. <code>
  98. 开启多域名支持,不同域名可以访问不同目录中的controller,在app.ini中的common下添加以下内容,注意:hosts中的内容会覆盖网站中对应的配置
  99. hosts[0.domain] = test.xxx.wang
  100. hosts[0.ext] = test
  101. hosts[1.domain] = admin.xxx.wang
  102. hosts[1.ext] = admin
  103. </code>
  104. <a id="modle"> 5) 数据库使用示例:</a>
  105. <code>
  106. 1、创建配置文件:configure/istudy.istudy_user.config.php,你可以设置好数据库配置文件或者在这里<a
  107. href="<?= $this->_request->getFullUrl('main') . '?url=' . urlencode($this->_request->getFullUrl('database/creator')); ?>"><b>生成</b></a>配置文件并下载保存到本地目录。
  108. <?php highlight_file('configure/istudy.istudy_user.config.php') ?>
  109. namespace Model;
  110. use \Qii\Model;
  111. class user extends Model
  112. {
  113. public function __construct()
  114. {
  115. parent::__construct();
  116. }
  117. public function saveUserInfo()
  118. {
  119. $user = (new \Qii\Driver\Easy())->_initialize();
  120. //设置数据表的主键,保存的时候会自动验证指定数据是否已经存在
  121. $user->setPrivateKey(array('email'));
  122. //设置规则
  123. $user->setRules(new \Qii\Driver\Rules(\Qii\Import::includes('configure/istudy.istudy_user.config.php')));
  124. $user->nickname = 'antsnet';
  125. $user->sex = 1;
  126. $user->email = 'antsnet4@163.com';
  127. $user->password = 'A123456a';
  128. $user->add_time = time();
  129. $user->update_time = time();
  130. $user->status = 0;
  131. //此处返回response对象
  132. $response = $user->_save();
  133. if($response->isError())
  134. {
  135. return $response->getErrors();
  136. }
  137. return array('uid' => $response->getResult('_result'));
  138. }
  139. /**
  140. * 获取用户信息
  141. */
  142. public function userInfo($email)
  143. {
  144. return $this->db->getRow('SELECT * FROM istudy_user WHERE email = "'.$this->db->setQuote($email).'"');
  145. //或者
  146. return $this->db->where(array('email' => $email))->selectOne('istudy_user');
  147. //或者使用以下代码
  148. $user = (new \Qii\Driver\Easy('istudy_user'))->_initialize();
  149. //设置规则
  150. $user->setRules(new \Qii\Driver\Rules(\Qii\Import::includes('configure/istudy.istudy_user.config.php')));
  151. //操作的时候会根据规则自动验证对应字段是否符合指定规则,不符合就不做查询操作
  152. $response = $user->_getRowByEmail('antsnet@163.com');
  153. if($response->isError())
  154. {
  155. return array('users' => array(), 'error' => $user->getErrors());
  156. }
  157. return array('users' => $response->getResult('_result'));
  158. }
  159. /**
  160. * 登录
  161. */
  162. public function login($email, $password)
  163. {
  164. $user = (new \Qii\Driver\Easy('istudy_user'))->_initialize();
  165. //设置规则
  166. $user->setRules(new \Qii\Driver\Rules(\Qii\Import::includes('configure/istudy.istudy_user.config.php')));
  167. $user->setPrivateKey(array('email'));
  168. $user->email = $email;
  169. $response = $user->_exist();
  170. if($response->isError())
  171. {
  172. return array('login' => false, 'error' => $response->getErrors());
  173. }
  174. $data = $response->getResult();
  175. if($data['password'] != md5($password))
  176. {
  177. return array('login' => false, 'error' => 'invalid password');
  178. }
  179. return array('login' => true, 'res' => $data);
  180. }
  181. public function update($email, $password)
  182. {
  183. $user = (new \Qii\Driver\Easy('istudy_user'))->_initialize();
  184. //设置规则
  185. $user->setRules(new \Qii\Driver\Rules(\Qii\Import::includes('configure/istudy.istudy_user.config.php')));
  186. $user->setPrivateKey(array('email'));
  187. $user->email = $email;
  188. $user->func('md5', 'password', $password);
  189. $response = $user->_update();
  190. if($response->isError())
  191. {
  192. return array('update' => false, 'msg' => $response->getErrors());
  193. }
  194. return array('update' => true);
  195. }
  196. public function remove($email)
  197. {
  198. $user = (new \Qii\Driver\Easy('istudy_user'))->_initialize();
  199. //设置规则
  200. $user->setRules(new \Qii\Driver\Rules(\Qii\Import::includes('configure/istudy.istudy_user.config.php')));
  201. $user->setPrivateKey(array('email'));
  202. $user->email = $email;
  203. $response = $user->_remove();
  204. if($response->isError())
  205. {
  206. return array('remove' => false, 'msg' => $response->getErrors());
  207. }
  208. return array('remove' => true);
  209. }
  210. }
  211. 使用方法:
  212. namespace controller;
  213. use \Qii\Controller_Abstract;
  214. class test extends Controller_Abstract
  215. {
  216. //启用模板引擎支持
  217. protected $enableView = true;
  218. //启用数据库支持
  219. protected $enableModel = true;
  220. //修改默认模板引擎
  221. protected $viewType = array('engine' => 'include');
  222. public function __construct()
  223. {
  224. parent::__construct();
  225. }
  226. public function indexAction()
  227. {
  228. $this->_model->getRow('SELECT * FROM user WHERE email = "antsnet@163.com"');
  229. print_r($this->_load->model('user')->saveUserInfo());
  230. print_r($this->_load->model('user')->userInfo('antsnet@163.com'));
  231. print_r($this->_load->model('user')->login('antsnet@163.com', 'A123456a'));
  232. print_r($this->_load->model('user')->update('antsnet@163.com', 'A123456a'));
  233. print_r($this->_load->model('user')->remove('antsnet@163.com'));
  234. }
  235. }
  236. </code>
  237. <a id="view"> 6) View的支持</a>
  238. <code>
  239. view支持smarty及php
  240. use \Qii\Controller_Abstract;
  241. class index extends Controller_Abstract
  242. {
  243. //启用view,在调用parent::__construct()时自动初始化view
  244. protected $enableView = true;
  245. //启用database在调用parent::__construct()时自动初始化database
  246. protected $enableModel = true;
  247. //修改默认模板引擎
  248. protected $viewType = array('engine' => 'include');
  249. public function __construct()
  250. {
  251. parent::__construct();//默认是app.ini中的view[engine],你可以在此处选择是用include或者require,只要将参数默认传给enableView即可
  252. $this->_view->display('tpl'); //$this->view即为使用的模板引擎
  253. }
  254. }
  255. </code>
  256. <a id="controller"> 7) Controller的使用</a>
  257. <code>
  258. namespace controller;
  259. use \Qii\Controller_Abstract;
  260. class test extends Controller_Abstract
  261. {
  262. //为了避免Controller逻辑过于复杂,可以将Action拆到单独的文件
  263. //当在调用dummy方法的时候会自动执行actions\dummy中的execute方法
  264. public $actions = array(
  265. "dummy" => "actions\dummy",
  266. );
  267. public function __construct()
  268. {
  269. parent::__construct();
  270. }
  271. public function indexAction()
  272. {
  273. //转发到\controller\test的indexAction方法上
  274. $this->dispatch('test', 'index');
  275. //执行结束后再转发
  276. $this->forward('test', 'index');
  277. }
  278. //在执行parent::__construct()的时候会自动调用,如果return false即直接退出程序
  279. protected function beforeRun()
  280. {
  281. return true;
  282. }
  283. /**
  284. * 执行完dispatch后调用
  285. */
  286. protected function afterRun()
  287. {
  288. }
  289. }
  290. </code>
  291. <a id="cache"> 8) Cache支持</a>
  292. <code>
  293. Controller中使用Cache
  294. namespace controller;
  295. use \Qii\Controller_Abstract;
  296. class cache extends Controller_Abstract
  297. {
  298. public function __construct()
  299. {
  300. parent::__construct();
  301. }
  302. public function cacheTest()
  303. {
  304. $cache_id = 'cache_test';
  305. //文件缓存
  306. $this->setCache('file', array('path' => 'tmp'));
  307. //Memcache缓存
  308. $this->setCache('memcache', array('servers' => array( array('host'=>'127.0.0.1','port'=>11211) )
  309. ,'life_time'=>600));
  310. //xcache
  311. $this->setCache('xcache', array('life_time'=>600));
  312. //缓存内容
  313. $this->_cache->set($cache_id, 'cache内容');
  314. //redis缓存
  315. $this->setCache('redis', array('servers' => array('127.0.0.1.6379')));
  316. $this->_cache->set($cache_id, array('cache' => 'cache 内容'));
  317. //获取缓存内容
  318. $this->_cache->get($cache_id);
  319. //移除缓存
  320. $this->_cache->remove($cache_id);
  321. }
  322. }
  323. Model中使用
  324. namespace Model;
  325. use \Qii\Model;
  326. class cache extends Model
  327. {
  328. public function __construct()
  329. {
  330. parent::__construct();
  331. }
  332. public function cacheTest()
  333. {
  334. $cache_id = 'cache_test';
  335. //文件缓存
  336. $this->setCache('file', array('path' => 'tmp'));
  337. //Memcache缓存
  338. $this->setCache('memcache', array('servers' => array( array('host'=>'127.0.0.1','port'=>11211) )
  339. ,'life_time'=>600));
  340. //xcache
  341. $this->setCache('xcache', array('life_time'=>600));
  342. //缓存内容
  343. $this->_cache->set($cache_id, 'cache内容');
  344. //获取缓存内容
  345. $this->_cache->get($cache_id);
  346. //移除缓存
  347. $this->cache->remove($cache_id);
  348. }
  349. }
  350. </code>
  351. </code>
  352. <a id="download">下载</a>
  353. <code>
  354. 测试阶段暂时不提供下载
  355. </code>
  356. </pre>
  357. <?php include(Qii_DIR . '/view/footer.php'); ?>
  358. </body>
  359. </html>