ThinkPHP5 development (2) graphic code explanation for implementing the registration function with background verification

Source: Internet
Author: User
The thinkphp5 manual provides many verification methods, which can be verified on the controller, helper functions, and model, however, for model verification, the User class inherits the verification class, but PHP does not support multi-inheritance. Which method is the most suitable.

Effect:

1. Database SQL script

-- PhpMyAdmin SQL Dump -- version 4.4.15.5 -- http://www.phpmyadmin.net---- Host: localhost -- Generation Time: 2016-07-09 13:12:42 -- Server Version: 5.7.12-log -- PHP Version: 7.0.7SET SQL _MODE = "NO_AUTO_VALUE_ON_ZERO"; SET time_zone = "+ 00:00 ";/*! 40101 SET @ OLD_CHARACTER_SET_CLIENT = @ CHARACTER_SET_CLIENT */;/*! 40101 SET @ OLD_CHARACTER_SET_RESULTS = @ CHARACTER_SET_RESULTS */;/*! 40101 SET @ OLD_COLLATION_CONNECTION = @ COLLATION_CONNECTION */;/*! 40101 set names utf8mb4 */; ---- Database: 'thinkphp5 '---- -------------------------------------------------------- TABLE structure 'think _ user' -- create table if not exists' think _ user' ('User _ id' int (11) not null, 'user _ name' varchar (255) not null, 'User _ sex' int (11) default null, 'User _ tel 'varchar (255) default null, 'user _ email 'varchar (255) default null, 'User _ address' varchar (255) default null, 'user _ Birth 'varchar (255) default null, 'User _ jointime' varchar (255) default null, 'User _ passwd' varchar (255) default null, 'user _ signature 'varchar (255) default null, 'User _ hobby' varchar (255) default null) ENGINE = InnoDB AUTO_INCREMENT = 3 default charset = utf8mb4; ---- 'think _ user' -- insert into 'think _ user' ('User _ id', 'User _ name', 'User _ sex ', 'user _ Tel', 'User _ email ', 'User _ address', 'User _ birth ', 'User _ jointime', 'User _ passwd', 'User _ signature ', 'User _ hobby') VALUES (1, 'thinkphp', 1, '123 ', 'emial @ email.com ', 'Shandong province, Jinan city, *** road', '000000', '000000', 'qqq', NULL, NULL), (2, 'pangpython ', 1, '000000', 'Email @ emial.com ', 'jinan city, Shandong province', '000000', NULL, '000000', 'nihao', 'uplink speed '); ---- Indexes for dumped tables ------ Indexes for table 'think _ user' -- alter table 'think _ user' ADD PRIMARY K EY ('User _ id '); ---- AUTO_INCREMENT for dumped tables ------ AUTO_INCREMENT for table 'think _ user' -- alter table 'think _ user' MODIFY 'user _ id' int (11) not null AUTO_INCREMENT, AUTO_INCREMENT = 3; /*! 40101 SET CHARACTER_SET_CLIENT = @ OLD_CHARACTER_SET_CLIENT */;/*! 40101 SET CHARACTER_SET_RESULTS = @ OLD_CHARACTER_SET_RESULTS */;/*! 40101 SET COLLATION_CONNECTION = @ OLD_COLLATION_CONNECTION */;

Code Directory structure:
View/regist/index.html

      
     Register      

3. write controller
Controller/Regist. php

 Fetch ('index');} // The User registers public function regist () {// instantiate user $ User = new User; // receives the data submitted by the front-end form $ user-> user_name = input ('post. user_name '); $ user-> user_sex = input ('post. userSex '); $ user-> user_tel = input ('post. userTel '); $ user-> user_email = input ('post. userEmail '); $ user-> user_address = input ('post. userAddress '); $ user-> user_birth = input ('post. userjointime'); $ user-> user_passwd = input ('post. userPas Swd'); $ user-> user_signature = input ('post. userSignature '); $ user-> user_holobby = input ('post. userhoby'); // perform rule verification $ result = $ this-> validate (['name' => $ user-> user_name, 'email '=> $ user-> user_email, 'Sex' => $ user-> user_sex, 'Tel '=> $ user-> user_tel, 'address' => $ user-> user_address, 'birth' => $ user-> user_birth, 'password' => $ user-> user_passwd,], ['name' => 'Require | max: 10', 'Email '=> 'emai L ', 'sex' => 'Number | between:', 'tel' => 'Require ', 'address' => 'Require ', 'birth' => 'Require ', 'password' => 'Require',]); if (true! ==$ Result) {$ this-> error ($ result);} // write the database if ($ user-> save ()) {return $ this-> success ('registered successfully');} else {return $ this-> success ('registered failed ');}}}

The thinkphp5 manual provides many verification methods, which can be verified on the controller, helper functions, and model, however, for model verification, the User class inherits the verification class, but PHP does not support multi-inheritance. Which method is the most suitable.
For details, the verification rules can be customized to use regular expressions to verify mobile phone numbers and ID card numbers.

Model/User. php

 

URL:
Http: // localhost/tp5/public/index. php/index/regist/

The above is ThinkPHP5 development (2) detailed description of the graphic code that implements the registration function with background verification. For more information, see other related articles in the first PHP community!

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.