Fifth day thinkphp Quick Stitching website (Fri)

Source: Internet
Author: User
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 Login Module Logi N method , mainly by changing the M method to the D method

 
  Assign (' title ', ' Backend management system '); $this->display ();} 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类,所以,实际上的代码可能和下面的等效:
                               Import (' @. Model.usermodel ');                               $User = new Usermodel ();
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, writing Customizing the user model class admin/lib/model/usermodel.class.php

 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 executes//array (' Fill field ', ' Fill content ', ' Fill condition ', ' Add rule ') on create,//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 a confirmation password and a 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, you 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.
Hint message: (must) the definition of the message that was used to validate the failure
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:


  • 其中,'__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 ');}

    The above on the fifth day thinkphp hands-on Quick splicing website (five), including the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.