Home > Backend Development > PHP Tutorial > Day 5 ThinkPHP step-by-step quick website splicing (5)

Day 5 ThinkPHP step-by-step quick website splicing (5)

WBOY
Release: 2016-08-08 09:22:25
Original
1051 people have browsed it

June 11th, sunny day. "Plums leave sore teeth, bananas are divided into green and window screens. As the day goes by, I wake up with ruthless thoughts and watch children catching willow flowers."

1. Login module Login method , mainly to change the M method to the D method

<?php
class LoginAction extends Action {
	function index() {
		// 配置页面显示内容
		$this->assign ( 'title', '后台管理系统' );
		$this->display ();
	}
	
	// 用户登录页面
	function login() {
		header ( "Content-Type:text/html; charset=utf-8" );
		
		$username=$_POST['username'];
		$password=md5($_POST['password']);
		
		$User = D ( "User" ); // 参数的User必须首字母大写,否则自动验证功能失效!
		if (! $User->create ()) {
			$this->error ( $User->getError () );
		} else {
			// 查找输入的用户名是否存在
			if ($User->where ( "username ='$username' AND password = '$password'" )->find ()) {
				session ( username, $username );
				$url = U ( '/Index/index/username/' . $username );
				redirect ( $url, 5, '跳转中...' );
			} else {
				$this->error ( '用户名或密码错误' );
			}
		}
}
Copy after login
Among them, $User = D('User');<span> means </span><span>Instantiate the User model, Will import the lib/model/usermodel.class.php file below the current item, and then instantize the UserModel class. Therefore, the actual code may be equivalent to the following: </span><span></span><span></span><div class="code" style="position:relative; padding:0px; margin:0px;"><pre name="code">&lt;span style=&quot;font-family:Comic Sans MS;font-size:14px;&quot;&gt; import('@.Model.UserModel'); $User = new UserModel();&lt;/span&gt;</pre><div class="contentsignin">Copy after login</div></div> <span>, create () create data creation data After the object is selected, the submitted form data will be automatically collected. </span><span>Before writing the form to the data table, there is often some data detection (whether the submitted user name meets the requirements) and processing (such as the comparison of the verification code in the example). The create() method supports automatic verification and automatic completion of data.使用 , In general, when using the M method to instantiate the model, it is impossible to instantiate the custom model class, and it is not possible to use automatic verification and automatic completion function. Therefore, it is recommended to use the D method to instantiate the model class. </span><span> </span>

2,
Written

Customized User model classadmin/Lib/Model/UserModel.class .php

<?php
/*
 * 自定义User模型类
 * 功能:1.完成自动验证功能
 */
class UserModel extends Model{		
	//自动验证成员属性(二维数组,每个数组代表一个验证规则)
	//array(&#39;验证字段&#39;,&#39;验证规则&#39;,&#39;错误提示&#39;,&#39;验证条件&#39;,&#39;附加规则&#39;,&#39;验证时间&#39;),
	//验证字段:表单中的字段名称,也可以是表单中的一些辅助字段,例如验证码,重复密码等
	//验证规则:
	//错误提示:出现错误,抛出一个什么样的提示告知用户
	//验证条件:参考手册6.15 (共有0,1,2三种值)
	//附加规则:比如使用正则表达式验证,callback函数验证等,默认使用正则验证
	//验证时间:1.新增时验证 2.编辑时验证 3.全部清况下验证
	protected $_validate=array(
		array(&#39;username&#39;,		&#39;require&#39;,		&#39;用户名必须非空&#39;),
		array(&#39;username&#39;,		&#39;callback_checklen&#39;,	&#39;用户名过长或过短&#39;,	0,	&#39;callback&#39;),
		array(&#39;password&#39;,		&#39;require&#39;,		&#39;密码必须非空&#39;	),
		array(&#39;repassword&#39;,	&#39;require&#39;,		&#39;请重复输入密码&#39;		),
	//	array(&#39;password&#39;,		&#39;repassword&#39;,&#39;两次输入的密码不一致,请重新输入&#39;,		0,	&#39;confirm&#39;),
		array(&#39;verify&#39;,&#39;require&#39;,&#39;验证码必须填写!&#39;),
		array(&#39;verify&#39;,&#39;callback_checkCode&#39;,&#39;验证码错误!&#39;,0,&#39;callback&#39;),  //使用回调函数checkCode
	);
	
	//字段长度验证回调函数(ThinkPHP会自动帮我们传递参数)
	function callback_checklen($data){
		if(strlen($data)>15 || strlen($data)<5){
			return false;
		}
		return true;
	}
	//验证码回调函数(ThinkPHP会自动帮我们传递参数)
	function callback_checkCode($data){
		if(md5($data)!=$_SESSION[&#39;verify&#39;]){
			return false;
		}
		return true;
	}
	
	//自动完成,在create时自动执行
	//array(&#39;填充字段&#39;,&#39;填充内容&#39;,&#39;填充条件&#39;,&#39;附加规则&#39;);
	//填充字段 
	protected $_auto=array(
			array(&#39;password&#39;,&#39;md5&#39;,3,&#39;function&#39;),
			array(&#39;ip&#39;,&#39;callback_returnip&#39;,1,&#39;callback&#39;),
			array(&#39;createtime&#39;,&#39;time&#39;,1,&#39;function&#39;),
	); 
	
	function callback_returnip(){
			return $_SERVER[&#39;REMOTE_ADDR&#39;];	
	}
}
?>
Copy after login
                                                                                                                                 The definition format is:
array(     
        array(验证字段1,验证规则,错误提示,[验证条件,附加规则,验证时间]),    
        array(验证字段2,验证规则,错误提示,[验证条件,附加规则,验证时间]),    
 ...... );
Copy after login
Description Auxiliary fields, such as confirm password and verification code, etc. When there are individual validation rules that have nothing to do with fields, the validation fields can be set at will. For example, expire validity rules have nothing to do with form fields. If field mapping is defined, the validation field name here should be the actual data table field rather than the form field. Verification rules: (must) rules to be verified need to be combined with additional rules. If additional rules for regular verification are used, the system also has built-in some commonly used regular verification rules, which can be used directly as verification rules, including: The require field must be email address, url URL address, currency currency, and number. Prompt message: (must) used to define the prompt message after verification fails Verification conditions: (optional) including the following situations: Model::EXISTS_VALIDATE or 0 Validate if the field exists (default) Model::MUST_VALIDATE or 1 Must be verified Model::VALUE_VALIDATE or 2 Verify when the value is not empty
3.

Writedatabase table
think_news





CREATE TABLE `think_news` (
  
	`id` int(11) NOT NULL AUTO_INCREMENT,
  
	`author` int(11) NOT NULL,
 
	`subject` varbinary(256) NOT NULL,
 
	`createtime` int(11) NOT NULL,
 
	`lastmodifytime` int(11) NOT NULL,
  
	`message` mediumtext NOT NULL,
  
	 PRIMARY KEY (`id`)

) ENGINE=MyISAM DEFAULT CHARSET=utf8;
Copy after login
5. Modify adminTplIndexindex.html Modify the edit news button as follows:
 <li><a class="shortcut-button" href="__URL__/news"><span> <img src="__PUBLIC__/Images/admin/icons/pencil_48.png" alt="icon" /><br />
        编辑新闻 </span></a></li>
Copy after login
    Among them, '__PUBLIC__' represents the site public directory, '__URL__' represents the URL address of the current module 6. In adminLibActionIndexAction.class. Add function news in php

function news(){
		//跳转到News控制器的index方法
		redirect(U('/News/index'),0, '编写新闻');
	}
	
Copy after login

The above introduces the fifth day of ThinkPHP step-by-step quick website splicing (5), including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template