PHP框架Yii编码规范

时间:2022-10-21 07:47:36

PHP框架Yii编码规范

文件方法命名

 

文件名即类名

类名称:

驼峰式 首字母大字

class PointController

class PointRatioController

公共成员方法:

驼峰式 首字母小写

public function getPointById()

私有成员方法:

驼峰式 首字母小写

private function _getPointById()

公共成员变量:

public $users

public $userName

私有成员变量:

private $_user

private $_userName

常量:

所有字母大写,单词间用下划线分隔

const POS_HEAD = 0;

代码编写规范

1. 多使用配置文件,避免硬编码

2. 多采用设计模式(工厂模式,单例模式等)

3. 使用注释

a)   对类名进行注释

b)   方法名进行注释(包括每个方法的参数与返回值)

c)   对常量进行注释

d)   对成员属性进行注释(public $userName; // 用户名)

e)   配置文件进行注释

Yii框架MVC模式:

model:

与数据持久层的交互

必要方法:

model();      // 静态方法 返回模型的实例

tableName(); // 返回数据库表名

rules();      // 表单的各种验证规则(用户名,email)

relations();  // 表关系配置(n:1 or 1:1 or n:n)

attributeLabels();  // 表字段国际化处理

search();           // 默认搜索方法

 

… 其他增删改查验证方法

 

controller:

主要处理用户的请求与响应

必要方法:

filters();           // 方法过滤

accessRules();       // 动作访问控制

actions();           // 动作action配置

 

… 其他处理请求与响应的方法

 

view:

采用Yii自带的组件模块

 

service:

对于业务逻辑比较复杂的系统,在增加业务逻辑层(service);程序员*发挥的地方;

主要处理比较复杂的业务逻辑

命名规范:UsersService

 

执行流程:[注:service层用于逻辑比较复杂的时候]

views->controller->service->model à用户请求

model->service->controller->views à系统响应

数据库访问

 

1. 数据访问对象(DAO)

复杂sql语句查询时使用

$user = Yii::app()->db->createCommand()

    ->select('id, username, profile')

    ->from('tbl_user u')

    ->join('tbl_profile p', 'u.id=p.user_id')

    ->where('id=:id', array(':id'=>$id))

 ->queryRow();

2. ActiveRecord

简单sql语句查询及添加修改删除时使用

Admin::model()->find(‘id=:id’, array(‘:id’=>id));

注:查询条件使用占位符的方式

 

需求整理

???

Yii常用方法

CHtml

Static Methods

For CActiveRecords:

activeCheckbox($m, $a);

activeCheckboxList($m, $a, $data);

activeDropDownList($m, $a, $data);

activeFileField($m, $a);

activeHiddenField($m, $a);

activeId($m, $a);

activeLabel($m, $a);

activeLabelEx($m, $a);

activeListBox($m, $a, $data);

activePasswordField($m, $a);

activeRadioButton($m, $a);

activeRadioButtonList($m, $a, $data);

activeTextArea($m, $a);

activeTextField($m, $a);

 

CFormModel

Properties

attributes, errors, safeAttributeNames, scenario, validators

Public Methods Events

addError($a, $error);

afterValidate($scenario);

attributeLabels();

beforeValidate($scenario);

hasErrors($a);

rules();

validate($s='', $a=NULL);

onAfterValidate($e);

onBeforeValidate($e);

 

CDbConnection

Properties

active, autoCommit, autoConnect, charset, commandBuilder,

connectionStatus, connectionString, currentTransaction, lastInsertId,

password, schema, schemaCachingDuration, timeout, username

For CForm

checkbox($n, $v);

checkboxList($n, $v, $data);

dropDownList($n, $c, $data);

leField($n, $v);

hiddenField($n, $v);

label($n, $v);

listBox($n, $v, $data);

passwordField($n, $v);

radioButton($n, $c);

radioButtonList($n, $m, $data);

textArea($n, $v);

textField($n, $v);

 

Yii (extends YiiBase)

Static Methods

app() /*gets the application instance */

t($category, $message, $params=array ( ),$source=NULL, $language=NULL);

import(string $alias, boolean $forceInclude=false);

log($msg, $level='info', $category='application');

 

CController (extends CBaseController)

Properties

action, defaultAction, id, layout, module, pageTitle

Methods

actions(); /*list of external actions*/

beginClip($id, $properties=array ( ));

endClip($idclip=’’);

beginWidget($className, $properties=array ( ));

endWidget(string $id='');

missingAction(string $actionID);

redirect($url, $terminate=true, $statusCode=302);

refresh($terminate=true,$anchor='');

render($view, $data=NULL, $return=false);

renderPartial($v, $d=NULL,$r=false, $processOutput=false);

widget( $className, $properties=array ( ));

 

CWebApplication (extends CApplication)

Core application components

errorHandler, securityManager, statePersister,

cache, messages, coreMessages

Properties

cache, charset, components, db, language, locale,

name, modules, securityManager, sourceLanguage

Methods

getNumberFormatter(); getDateFormatter();

 

CWidget (extends CBaseController)

Properties

controller, id, owner, viewPath

Methods

init(); run(); render($view, $data=NULL, $return=false);

Public Methods

beginTransaction();

createCommand($sql);

open();

close();

init();

quoteColumnName($name);

quoteTableName($name);

quoteValue($name);

 

CHttpRequest

Properties

baseUrl, browser, cookies, hostInfo, isAjaxRequest,

isPostRequest, isSecureConnection, pathInfo,

preferredLanguage, queryString, requestUri,

serverName, serverPort, url, urlReferrer, userAgent,

userHost, userHostAddress

Methods

sendFile($fn, $c, $mime=NULL, $t=true);

 

CActiveRecord

Properties

attributes, errors, isNewRecord, primaryKey, scenario

Static Methods

model($className='CActiveRecord');

Public Methods Events

count($c=’’, $p=array ( ));

delete();

deleteAll($c='', $p=array ( ));

nd($c='', $p=array ( ));

ndAll($c='', $p=array ( ));

ndBySql($sql, $p=array ( ));

hasErrors($a);

update($a=NULL);

updateAll($a, $c='', $p=array ( ));

updateByPk($pk, $a, $c='', $p=array ( ));

validate();

onAfterDelete($e);

onAfterInsert($e);

onAfterSave($e);

onAfterValidate($e);

onBeforeDelete($e);

onBeforeSave($e);

onBeforeValidate($e);

 

CDbCommand

Properties

connection, text

Public Methods

bindParam($n, $v, $dt=NULL, $length=NULL);

bindValue($n, $v, $dataType=NULL);

execute();

query();

queryAll($fetchAssociative=true);

queryColumn();

queryRow($fetchAssociative=true);

queryScalar();