PHP Yii开源框架入门学习(一)
Jun 13, 2016 am 10:54 AM以下是给成员进行Yii框架培训写的一些内容。
1) 下载Yii 1.1.12:
2) 解压到 /var/www/html, 并将目录重命名为yii;
3) 检查电脑环境是否符合yii要求,不符合请安装所缺软件;若显示php pdo未成功,则请检查php.ini配置项是否和1.9中一致;
4) 为方便查看Yii框架中的例子程序,可在php配置文件中将sqlite数据库支持加上,重启Apache生效:
extension=php_pdo_sqlite.dll
5) 打开Yii自带的程序和网站,研究它的结构和程序:
http://127.0.0.1/yii/demos/helloworld/
http://127.0.0.1/yii/demos/blog/
等等
6) 使用Yii工具生成一个模板网站:
打开命令行工具:开始—>运行, 命令如下:
C:\Users\bihhe>d:
D:\>cd /var/www/html/yii/framework
D:\var\www\html\yii\framework>/var/php53/php /var/www/html/yii/framework/yiic.php webapp /var/www/html/test1
打开浏览器输入http://127.0.0.1/test1/index.php 即可访问创建的网站。
7) 创建数据库表:
CREATE TABLE `test1`.`test1_userinfo` (
`id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
`uname` VARCHAR(45) NOT NULL,
`upass` VARCHAR(45) NOT NULL,
`count` INTEGER UNSIGNED,
PRIMARY KEY (`id`)
)
ENGINE = InnoDB;
8) 修改模板网站的数据库连接方式,修改test1/protected/config/main.php 如下:
/*
'db'=>array(
'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db',
),
*/
// uncomment the following to use a MySQL database
'db'=>array(
'connectionString' => 'mysql:host=127.0.0.1;dbname=test1',
'emulatePrepare' => true,
'username' => 'root',
'password' => 'password',
'charset' => 'utf8',
'tablePrefix'=>'test1_',
),
配好之后我们就可以在任何地方使用 Yii::app()->db调用该数据库连接了。
9) 使用Gii工具生成Model和CRUD(增删查改):
编辑protected/config/main.php 如下:
return array(
......
'import'=>array(
'application.models.*',
'application.components.*',
),
'modules'=>array(
'gii'=>array(
'class'=>'system.gii.GiiModule',
'password'=>'pick up a password here',
),
),
);
打开网页:http://127.0.0.1/index.php?r=gii
输入密码进入,选择生成model,输入表名:test1_userinfo, Model类名:TUserInfo;
点击生成,Model类将生成在:test1\protected\models
同样的方式选择生成CRUD,代码生成在:
test1\protected\controllers
test1\protected\views\tUserInfo
研究test1\protected\controllers\ TUserInfoController.php及其它生成的网页
输入网址即可访问刚才生成的网页: http://127.0.0.1/test1/index.php?r=tuserinfo
添加模块:打开网页:http://127.0.0.1/index.php?r=gii
输入密码进入,选择生成module,输入UserInfo模块名,点击生成;网页生成在:
test1\protected\modules\UserInfo
编辑main.php如下:
'modules'=>array(
// uncomment the following to enable the Gii tool
'gii'=>array(
'class'=>'system.gii.GiiModule',
'password'=>'nokialab',
// If removed, Gii defaults to localhost only. Edit carefully to taste.
'ipFilters'=>array('127.0.0.1','::1'),
),
'UserInfo',
),
输入网址http://127.0.0.1/test1/index.php?r=UserInfo/default/index 即可访问新生成的模块。
10) 在Eclipse中导入项目: 请确保eclipse版本为helios for php版本
在项目根目录下创建 .project文件,内容为:
在项目根目录下创建 .buildpath 文件,内容为:
打开eclipse,File à Import à Existing Project into Workspace à Next à Select root directory, 选择项目目录,继续即可。
11) Yii页面包括view、layout若有中文,请把页面保存格式改为utf-8,否则中文显示将成乱码;

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

How To Set Up Visual Studio Code (VS Code) for PHP Development
