yii框架入门学习笔记六 其他功能

    前面的工作将blog的基本功能完成了。之前学习tp框架时,tp的完全手册非常的好用,yii的手册相比就更php的手册一样,当字典来学习可以,用来按部就班的学习貌似不可行。所以这次我想先照着教程完成一个yii框架的系统,对yii的使用有个初步的了解。

    这一篇我没有变做demo边写博文,而是整体完成之后写的。之前照着教程敲时,还有很多地方不太立即,做完这部分后扫清了不少盲点。

    有一点之前没想明白,后来到做日志功能完善的时候想明白了,不知道那时候有没有写,

    首先是侧边栏的定义

 

 

侧边栏包含三项:用户菜单、标签列表、最新评论列表。这三部分都使用了不同的portlet,他们都继承与CPortlet。

使用三个不同的portlet需要建立三个不同的类都放在compents下:

View Code
 1 UserMenu.php
 2 <?php
 3 Yii::import('zii.widgets.CPortlet');
 4 class UserMenu extends CPortlet{
 5     public function init() {
 6         $this->title = CHtml::encode(Yii::app()->user->name);
 7         parent::init();
 8     }
 9     
10     protected function renderContent() {
11         $this->render('userMenu');
12     }
13 }
14
View Code
 1 TagCloud.php
 2 <?php
 3 Yii::import('zii.widgets.CPortlet');
 4 class TagCloud extends CPortlet{
 5     public $title = 'Tags';
 6     public $maxTags = 20;
 7     
 8     protected function renderContent() {
 9         $tags = Tag::model()->findTagWeights($this->maxTags);
10         
11         foreach ($tags as $tag => $weight) {
12             $link = CHtml::link(CHtml::encode($tag), array('post/index', 'tag' => $tag));
13             echo CHtml::tag('span', array(
14                 'class' => 'tag',
15                 'style' => "font-size:{$weight}pt",
16             ), $link) . "\n";
17         }
18     }
19 }
20 
View Code
 1 RecentComments.php
 2 <?php
 3 Yii::import('zii.widgets.CPortlet');
 4 class RecentComments extends CPortlet{
 5     public $title = '最新评论';
 6     public $maxComments = 10;
 7     
 8     public function getRecentComments(){
 9         return Comment::model()->findRecentComments($this->maxComments);
10     }
11     
12     protected function renderContent() {
13         $this->render('recentComments');
14     }
15 }
16 
17 ?>

 其中标签云和另外两个不太一样,用户菜单和最新评论的直接在类中渲染视图。

View Code
1 userMenu.php
2 <ul>
3     <li><?php echo CHtml::link('新建日志', array('post/create'));?></li>
4     <li><?php echo CHtml::link('管理日志', array('post/admin'));?></li>
5     <li><?php echo CHtml::link('审核评论', array('comment/index')).'('.Comment::model()->getPendgingCommentCount().')';?></li>
6     <li><?php echo CHtml::link('注销', array('site/logout'));?></li>
7 </ul>
View Code
1 recentComments.php
2 <ul>
3     <?php foreach($this->getRecentComments() as $comment):?>
4     <li>
5         <?php echo $comment->authorLink;?>on
6         <?php echo CHtml::link(CHtml::encode($comment->post->title), $comment->getUrl());?>
7     </li>
8     <?php endforeach;?>
9 </ul>

 标签云在渲染展示前需要添加相关链接。(老实来讲,这部分我还是没打搞懂,照着教程来的。)

使用这些小物件的方法是修改模板页面的cloun2.php文件

1 <div id="sidebar">
2     <?php if(!Yii::app()->user->isGuest) $this->widget('UserMenu');?>
3             <?php $this->widget('TagCloud', array(
4                 'maxTags' => Yii::app()->params['tagCloudCount'],
5             ));?>
6             <?php $this->widget('RecentComments', array(
7                 'maxComments' => Yii::app()->params['recentCommentsCount'],
8             ));?>
9     </div><!-- sidebar -->

 这里做了一个简单的处理,只有用户登录之后,才可以显示用户菜单,这样一个简单的实用的权限控制方式完成了。本来还想根据代码梳理一下yii视图的渲染展现方式,发现现在还理不清,先放着吧。剩下的事情基本就是修改配置文件了。

关于url链接地址美化

1 'components'=>array(
2         'urlManager'=>array(
3             'urlFormat'=>'path',
4             'rules'=>array(
5                             'post/<id:\d+>/<title:.*?>' => 'post/view',
6                             'posts/<tas:.*?>' => 'post/index',
7                             '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
8             ),
9         ),

 对url链接的重写可以更加有利于seo的优化

 

 现在由于没有定义默认主页,打开页面时会跳转至indexcontroller下actionindex渲染的页面,我们将默认主页转向post

 

'defaultController'=>'post',

 至此,yii搭建的blog已经完成。

 

点击此处下载源代码

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2013-04-26 16:38  albafica  阅读(573)  评论(0编辑  收藏  举报