PHP框架Yii編碼規範

PHP框架Yii編碼規範程序員

文件方法命名sql

 

文件名即類名數據庫

類名稱:設計模式

駝峯式 首字母大字cookie

class PointControllerapp

class PointRatioController框架

公共成員方法:fetch

駝峯式 首字母小寫ui

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();

相關文章
相關標籤/搜索