PHP Framework YII Coding Specification

Source: Internet
Author: User
Tags php framework yii

PHP Framework Yii Coding Specification

File method naming

The file name is the class name

Class Name:

Camel-Type Initial characters

Class Pointcontroller

Class Pointratiocontroller

Public member methods:

Camel First Letter Lowercase

Public Function Getpointbyid ()

Private Member Methods:

Camel First Letter Lowercase

Private Function _getpointbyid ()

Public member variables:

Public $users

Public $userName

Private member variables:

Private $_user

Private $_username

Constant:

All uppercase letters, separated by underscores between words

Const POS_HEAD = 0;

Code Authoring Specification

1. use more configuration files to avoid hard coding

2. Multi-use design mode ( Factory mode, single case mode, etc. )

3. using annotations

a) comment on the class name

b) The method name is commented ( including the parameter and return value of each method )

c) comment on a constant

d) comment on member properties (public $userName;// user name )

e) configuration file for comments

Yii Framework MVC pattern:

Model :

Interaction with the data persistence layer

Necessary methods:

Model (); Static methods return instances of the model

TableName (); Returns the database table name

Rules (); Various validation rules for forms (user name, email)

Relations (); Table-Relationship configuration (N:1 or 1:1 or N/a)

Attributelabels (); table field Internationalization processing

Search (); Default Search method

... Other additions and deletions to check the verification method

Controller :

Primary processing of user requests and responses

Necessary methods:

Filters (); Method filtering

Accessrules (); Action Access Control

Actions (); Action Action Configuration

... Other methods for handling requests and responses

View :

Modular module with YII self-assembly

Service :

For systems with complex business logic, the Business Logic Layer (service) is added, where the programmer is free to play;

Dealing with more complex business logic

Naming specification: Usersservice

Execution process:[ Note: Service layer is used when logic is complex]

Views->controller->service->model à user request

Model->service->controller->views à system response

Database access

1. Data Access Objects (DAO)

Complex SQL statement queries are used

$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

Simple SQL statement Query and add modify delete use

Admin::model ()->find (' Id=:id ', Array (': Id ' =>id));

Note: The way a query condition uses placeholders

Demand Consolidation

???

Common methods of 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);

Widgets ($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 Framework YII Coding Specification

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.