PHP架構Yii編碼規範

來源:互聯網
上載者:User

標籤:cWeb   style   http   io   ar   color   os   使用   sp   

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. Data Access Objects(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();


PHP架構Yii編碼規範

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.