当前位置: 首页 > 工具软件 > YiiBlog > 使用案例 >

Yii blog 开发学习

端木皓君
2023-12-01

linux下安装
先把yii拷到发布目录下 之后执行如下语句
chmod -R 777 yii
/var/www/apache2-default/yii/framework/yiic webapp /var/www/apache2-default/test

在库里建立表且更改main.php中的连接库文件
/var/www/apache2-default/yii/framework/yiic shell /var/www/apache2-default/test/index.php
之后执行 (help model controller crud module)
model User
crud User这样就可以创建 关于user表的增删改查了

Windows下安装
1.先进入c盘中php的安装目录 要使用php这个解析器
c:\wamp\php>php e:\eth\yii\framework\yiic webapp e:\eth\yii\test

2. 新建立表后需要创建与库关联的文件 modules/user.php
c:\wamp\php>php e:\eth\yii\framework\yiic shell e:\eth\yii\test\index.php
. model User

 

blog/protected/controllers/CommentController.php 中public function actionList()
它的路径就是 http://localhost/yii/demos/blog/index.php/comment/list


---------------------------------------------------------------------

1.创建库
 /wwwroot/blog/protected/data.
 /wwwroot/yii/demos/blog/protected/data/schema.sqlite.sql.

2.设置库的连接文件
/wwwroot/blog/protected/config/main.php.
主文件index.php中Yii::createWebApplication($config)->run(); $config内容为main.php定义过的。其中'defaultController'=>'post', 表明第一次调用的controller
 
3.在/wwwroot/blog/protected/models中填写控制每个表格的sql语句
 在/wwwroot/blog/protected/controllers/中填写action语句
 在/wwwroot/blog/protected/view/中填写html

4.执行login工作 /wwwroot/blog/protected/components/UserIdentity.php
/wwwroot/blog/protected/models/LoginForm.php
包含rules()模块 用来定义标题长度等参数

 核心模块位置/wwwroot/framework/web/auth/CUserIdentity.php
 你可以在项目中增加所需要的模块 文件地址
 /wwwroot/blog/protected/config/main.php
'import'=>array(
 'application.models.*',
 'application.components.*',
),

Post management
rules()
 对每个输入框post过来的数据进行规范化
 当调用save() validate()时候,这个rule()才起作用


5./wwwroot/blog/protected/models/Comment.php
 中safeAttributes()表示哪些文本框的数据是可以信赖的
 $post->attributes=$ POST['Post'];


6.relations()相当于sql语句的作用
http://www.yiiframework.com/doc/guide/database.arr
'VarName'=>array('RelationType', 'ClassName', 'ForeignKey', ...additional options)
'author'=>array(self::BELONGS_TO, 'User', 'authorID'),

$posts=Post::model()->with(
 'author.profile',
 'author.posts',
 'categories')->together()->findAll();
 //用together节省效率
Without calling together, this will need two SQL queries

7./wwwroot/blog/protected/models/Post.php
 存库
 
8./wwwroot/blog/protected/controllers/PostController.php
 文件里存在accessRules()函数 用来控制权限

9./wwwroot/blog/protected/views/post/_form.php
 这个文件用来提交form

10. 在_form.php中需要提交创建此表格的时间和作者
 需要在/wwwroot/blog/protected/models/Post.php 文件中加入beforidate()函数来创建这2个属性
 afterSave()函数来写sql语句,进行入库操作

11. /wwwroot/blog/protected/controllers/PostController.php 在这个文件中的任何action与之对应的views文件夹中肯定有相应的文件比如create.php update.php
 中的actionUpdate()和actionCreate()进行前段数据的传递
 前段点击按钮后先触发$post->validate()检查数据是否符合规范,再触发$post->save()进行存储

12.Displaying Posts
/wwwroot/blog/protected/views/post/show.php
增加判断,若没有发布或者用户观看权限不够,就跳到错误页面上
CHtml::encode() 可以对html进行反编译
 
13.PostController.php中的actionList() actionAdmin()都是列表页面
可以结合相应的sql语句用$criteria=new CDbCriteria; 连接数据库
在提交按钮上进行添加
<?php echo CHtml::linkButton('Delete',array(
'submit'=>array('post/delete','id'=>$post->id),
'confirm'=>"Are you sure to delete this post?",
)); ?>
 
14.Deleting Posts删除页面 PostController.php中有actionDelect函数 但是view中没有此模版delete.php
blog例子中blog/protected/views/comment/_list.php 这个文件增加了此句话代替单独的模版

chapter 4
一般在models文件夹里的文件写如下函数

1.rules() 用户定制某项框的确认要求

2.safeAttributes()指定哪些输入框的信息可以被录入

3.relations()组合关联字段

4.attributeLabels() 定义属性名字

5.beforidate() 用$parser=new CMarkdownParser;高亮显示某消息
afterSave() afterDelete() 存储和删除
 
6.创建和展示评论
actionShow()中'comments'=>$post->comments,是与relation()函数相关后的结果
创建时候需要增加一个私有方法function newComment($post)
模版里增加表单
<?php $this->renderPartial('/comment/ form',array(
'comment'=>$newComment,
'update'=>false,
)); ?>

7.回复之后 增加一个approve选项代码增加actionApprove()
更改评论的状态
 
chapter5

1.blog/protected/components/Portlet.php
init() run()

2./wwwroot/blog/protected/views/layouts/main.php
 构架html的框架 包括如何引用到css

3.创建用户表单
 1 /wwwroot/blog/protected/components/UserMenu.php
继承Portlet类
 2 /wwwroot/blog/protected/components/views/userMenu.php
创建menu的HTML格式多用于超链接写入此文件中CHtml::link
创建一个按钮CHtml::linkButton
 3/wwwroot/blog/protected/views/layouts/main.php
嵌入这个菜单 <?php $this->widget('UserMenu',array('visible'=>!Yii::app()->user->isGuest)); ?>

4.创建login菜单
 1 /wwwroot/blog/protected/components/UserLogin.php
 继承Portlet类 创建一个$form=new LoginForm; 类这个是从/wwwroot/blog/protected/models/LoginForm.php 衔接过来的
 $this->render('userLogin',array('form'=>$form));
 2/wwwroot/blog/protected/components/views/userLogin.php
 创建view
 3/wwwroot/blog/protected/views/layouts/main.php
 主文件中嵌入,且添加逻辑。当是游客身份如何显示等

5.创建 tag
1/wwwroot/blog/protected/components/TagCloud.php
return Tag::model()->findTagWeights(); 先调用这个方法,它列出所有关联的tag
此函数在 blog/protected/models/Tag.php 中
2/wwwroot/blog/protected/components/views/tagCloud.php
 创建view
3/wwwroot/blog/protected/views/layouts/main.php
添加到主页面下
 
6.创建最新评论recent comment
1 /wwwroot/blog/protected/components/RecentComments.php
 return Comment::model()->findRecentComments(); 调用最新评论
 此函数在/blog/protected/models/Comment.php文件中

2/wwwroot/blog/protected/components/views/recentComments.php
创建view
3 添加到主文件
/wwwroot/blog/protected/views/layouts/main.php


chapter 6

1.美化URL,尽量避免这样/index.php?r=post/show&id=1。这样不利于SEO
 在blog/protected/config/main.php文件的'urlManager'=>array参数中进行修正


2.当已经登陆后,再次登陆要给出提示
 在blog/protected/config/main.php'log'=>array(进行添加

3.当有错误发生需要跳转到错误页面时候,错误页面存放地址
 /wwwroot/blog/protected/views/system

4.网站的第一个页面设置在blog/protected/config/main.php 的'defaultController'=>'post',
 
 
5.设置 Schema Caching
 blog/protected/config/main.php
 'cache'=>array('db'=>array(


6.用theme
/wwwroot/blog/themes/classic/views/layouts/main.php
return array(
'theme'=>'classic',

7.应用cache(COutputCache) /wwwroot/blog/protected/views/layouts/main.php

<?php if($this->beginCache('tagCloud', array('duration'=>3600))) { ?>
<?php $this->widget('TagCloud'); ?>
<?php $this->endCache(); }?>

 

 类似资料: