June 11, sunny day. "Plum leaves limp teeth, banana sub-green and window." Day long sleep without emotion, busy watching children catch willow flower. "
10, backstage writing-continued-2
1, change write The login method of the login module is mainly to change M method to D method
<?phpclass Loginaction extends Action {function index () {//config page display content $this->assign (' title ', ' admin system '); $this->d Isplay ();} User Login page Function login () {header ("content-type:text/html; Charset=utf-8 "); $username =$_post[' username '); $password =md5 ($_post[' password ']); $User = D (" User "); The user of the parameter must be capitalized first, or the automatic verification function will fail! if (! $User->create ()) {$this->error ($User->geterror ()),} else {//Find the user name entered if there is an if ($User->where ("Userna me = ' $username ' and password = ' $password ' ")->find ()) {session (username, $username); $url = U ('/index/index/usern ame/'. $username); Redirect ($url, 5, ' jump ... ');} else {$this->error (' Username or password error ');}}}
among them, the
$User = D(‘User‘);
expression
实例化User模型,会导入当前项目下面的Lib/Model/UserModel.class.php文件,然后实例化UserModel类,所以,实际上的代码可能和下面的等效:
<span style= "Font-family:comic Sans ms;font-size:14px;" > import (' @. Model.usermodel '); $User = new Usermodel ();</span>
after Create () creates a data object, the submitted form data is automatically collected. before writing a form to a data table, there are often some checks on the data (whether the submitted user name meets the requirements) and the processing (as in the case of the Verification Code). The Create () method supports automatic validation and automatic completion of data.
In General, when you instantiate a model using the M method, you cannot instantiate a custom model class, and you cannot use automatic validation and AutoComplete, so we recommend using the D method to instantiate the model class.
2. Write Customizing the User model classadmin/lib/model/usermodel.class.php
<?php/* * Custom User model class * Features: 1. Complete the automatic validation function */class Usermodel extends model{//automatically validates member properties (two-dimensional arrays, each of which represents a validation rule)//array (' Validation field ', ' Validation rules ', ' Error hints ', ' validation conditions ', ' additional rules ', ' Validation time '),//Validation fields: field names in the form, or some auxiliary fields in the form, such as verification codes, duplicate passwords, etc.//validation rules://Error Hint: Error occurred, throw a prompt to inform the user Verification Conditions: Reference manual 6.15 (total 0, 1, 23 values)//Additional rules: for example, using regular expression validation, callback function validation, and so on, the default use of regular validation//authentication time: 1. Validation 2. Validation at edit 3. Validate protected under all conditions $_ Validate=array (' username ', ' require ', ' user name must be non-null '), Array (' username ', ' callback_checklen ', ' User name too long or too short ', 0, ' Callback '), Array (' Password ', ' Require ', ' password must be non-null '), array (' Repassword ', ' require ', ' repeat-enter password '),//array (' Password ', ' Repassword ', ' two times the password entered is inconsistent, please re-enter ', 0, ' confirm '), array (' Verify ', ' Require ', ' verification code must be filled in! '), Array (' Verify ', ' Callback_ Checkcode ', ' captcha error! ', 0, ' callback '),//Use callback function Checkcode);//Field length validation callback function (thinkphp will automatically pass parameters to us) function Callback_checklen ($data) {if (strlen ($data) >15 | | strlen ($DATA) <5) {return false;} return true;} Verification Code callback function (thinkphp will automatically pass parameters) function Callback_checkcode ($data) {if (MD5 ($data)!=$_session[' verify ']) {return false ;} return true;} Auto-complete, automatically execute at create Time//Array (' Fill field ', ' Fill content ', ' Fill condition ', ' additional rules ');//Fill field protected $_auto=array (' Password ', ' MD5 ', 3, ' function '), array (' IP ' , ' Callback_returnip ', 1, ' callback '), Array (' Createtime ', ' time ', 1, ' function '),); function Callback_returnip () {return $_server[' remote_addr '];}}? >
validation rules are defined as uniform rules that are defined in the following format:
Array ( Verify field 1, validation rule, error prompt, [validation condition, additional rule, validation time]), Array (validation field 2, validation rule, error prompt, [validation condition, additional rule, validation time]), ...);
description
Validation Fields: (must) the name of the form field that needs to be validated, which is not necessarily a database field, or it can be a secondary field for a form, such as confirmation password and verification code, and so on. Validation fields can be set arbitrarily if there are individual validation rules and field-independent conditions, such as expire expiration rules that are not related to form fields.
If a field mapping is defined, the validation field name here should be the actual table field instead of the form field.
Validation rules: (must) to validate the rules, need to combine additional rules, if the use of regular validation of additional rules, the system also built in some common regular validation rules, can be used as a validation rule, including: Require field must, email mailbox, url URL address, Currency currency, number numbers.
Prompt Information: (required) for the definition of the message after validation failed
Validation Criteria: (optional) contains the following scenarios:
Model::exists_validate or 0 exists field is verified (default)
Model::must_validate or 1 must be verified
Model::value_validate or 2 value is not empty when validating
3, write database table think_news
CREATE TABLE ' think_news ' ( ' id ' int (one) not null auto_increment, ' author ' int (one) not null, ' subject ' varbinary (2 ) not NULL, ' createtime ' int (one) not null, ' lastmodifytime ' int (one) not NULL, ' message ' Mediumtext NOT NULL, PRI MARY KEY (' id ')) engine=myisam DEFAULT Charset=utf8;
5, modify the admin\tpl\index\index.html
Modify the Edit News button as follows:
<li><a class= "Shortcut-button" href= "__url__/news" ><span> <br/> Edit News </span></a></li>
其中,‘__PUBLIC__‘表示
站点公共目录,‘__URL__‘
表示当前模块的URL地址
6. Add a function in admin\lib\action\indexaction.class.php news
Function News () {//jump to the News controller's Index method redirect (U ('/news/index '), 0, ' Write News ');}
Fifth day thinkphp Quick Stitching website (Fri)