php的yii框架开发总结6

MVC中的Controller部分,所有的controller类都是继承自Controller基类,基类里面包含actionAdmin-管理员,actionIndex-一般默认显示,actionView-查看某些信息,actionDelete-删除数据,actionUpdate-修改信息,actionCreate-创建基于model某条信息,当然我们还可以自己构建我们需要的方法,并配合自己写的View以得到其它功能页面。下面我们来详细介绍Controller里面的方法、属性。

public $layout='//layouts/column2';//这是CController基类里的一个属性,是布局用的,会在view文件中用到;

下面这个方法是对该页面下的各个功能的权限设置:

 1 public function accessRules()
 2     {
 3         return array(
 4             array('allow',  // allow all users to perform 'index' and 'view' actions
 5                 'actions'=>array('index'),
 6                 'users'=>array('*'),
 7             ),
 8             array('allow', // allow authenticated user to perform 'create' and 'update' actions
 9                 'actions'=>array('view','create','update','show'),
10                 'users'=>array('@'),
11             ),
12             array('allow', // allow admin user to perform 'admin' and 'delete' actions
13                 'actions'=>array('admin','delete','open'),
14                 'users'=>array('admin'),
15             ),
16             array('deny',  // deny all users
17                 'users'=>array('*'),
18             ),
19         );
20     }

*代表全部,也就是所有用户都能看到index页面,包括游客guest;@代表注册用户,也包括admin,他能看到view、create、update、show还有前面都有的index页面,admin就是管理员,他除了能享受上面的服务之外还能有权看admin、delete、open下的页面,其他的功能deny所有用户。

public function actionView()
    {
        $this->render('view',array(
            'model'=>$this->loadTodayModel(),
        ));
    }
public function loadTodayModel()
    {
        $model=Dailyreport::model()->findBySql("select *from tbl_dailyreport where author_id=:id and datediff(create_time,curdate())=0",array(':id'=>Yii::app()->user->id));
        if($model===null)
            throw new CHttpException(404,'The requested page does not exist.');
        return $model;
    }

这是查看当前用户今天所发日报的操作,loadTodayModel()方法中我怕们用了findBySql()方法,该方法返回满足条件的单条记录,其中datediff(create_time,curdate())=0是判断“日期是否是今天”的方法,得到model后render到view视图,我们看一下view视图怎么定义的:

<?php
/* @var $this DailyreportController */
/* @var $model Dailyreport */

$this->breadcrumbs=array(
    '日报列表'=>array('index'),
    $model->id,
);

$this->menu=array(
    array('label'=>'日报列表', 'url'=>array('index')),
    array('label'=>'发日报', 'url'=>array('create')),
    array('label'=>'修改日报', 'url'=>array('update', 'id'=>$model->id)),
);
?>

<h1>查看日报</h1>

<?php $this->widget('zii.widgets.CDetailView', array(
    'data'=>$model,
    'attributes'=>array(
        'id',
        array(
            'label'=>'内容',
            'value'=>$model->content,
        ),
        array(
            'label'=>'发布时间',
            'value'=>$model->create_time,
        ),
        array(
            'label'=>'作者',
            'value'=>Yii::app()->user->name,
        ),
    ),
)); ?>
breadcrumbs是网页面包屑,$model就是从Controller传过来的model值,menu就是页面是一些链接,可以指到其它view,有些需要参数,比如上面的update,
参数是赋值给actionUpdate()方法的。下面用到了一个widget组件CDetailView:'data'是它的属性值,含义:the data model whose details are to be displayed.
也可以是其它值,参见API。attributes格式:
array(
            'name'=>'create_time',//name是必须有的,对应model中的字段,下面三个属性并不是必须的
            'type'=>'date',//显示数据的格式,有很多种raw, text, ntext, html, date, time, datetime, boolean, number, email, image, url.
            'label'=>'发布时间',//在页面上显示的header,也就是标题
            'value'=>$model->create_time,//显示的数值
        ),
另外:usernameid 属性都将存储在用户 SESSION 中,可在代码的任何部分通过 Yii::app()->user 访问。Yii::app()->user->name,Yii::app()->user->id即可
得到当前登录用户的名字和ID,非常常用的。
其它的方法同理,但也有所不同。
原文地址:https://www.cnblogs.com/nannanITeye/p/3249016.html