Home > PHP Framework > ThinkPHP > Teach you how to understand the thinkphp framework from scratch?

Teach you how to understand the thinkphp framework from scratch?

慕斯
Release: 2021-06-16 09:34:24
forward
3004 people have browsed it

This article will share with you how to understand the thinkphp framework from scratch? (Sharing) has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

Teach you how to understand the thinkphp framework from scratch?

First, download the latest version of thinkphp, version 3.2.2, Download address http://www.thinkphp .cn/donate/download/id/502.html. Create a project, put all the files after decompression of the compressed package into the project, then configure the apach server, open the browser, and output the URL localhost. The page will prompt "Welcome to thinkphp", which means the application is successful.

1. Create the entry file

Then create a new folder under the project. For example, to create a shopping website, it is recommended to create a shopping folder and create a new index under the folder. .php, introduce the Thinkphp.php file inside.

<?php
 include &#39;../ThinkPHP/ThinkPHP.php&#39;;
?>
Copy after login

Then visit the website localhost/shopping/index.php and it will also display "Welcome to thinkphp". At this time, you will find that there are several more folders in the directory you created, common, component, Home, Runtime these folders.

2. Database connection

We first open the Common folder, then open the Conf folder, there is a config.php file in it, and then open the file :

<?php
return array(
	//&#39;配置项&#39;=>&#39;配置值&#39;
	&#39;URL_MODEL&#39;        => 1,
	&#39;SHOW_PAGE_TRACE&#39;  =>false,
	&#39;TMPL_ENGINE_TYPE&#39;		=>  &#39;Smarty&#39;,
	&#39;SESSION_AUTO_START&#39; =>true,
	&#39;URL_CASE_INSENSITIVE&#39;   => false,
	&#39;DB_TYPE&#39;               =>  &#39;mysql&#39;,     // 数据库类型
    &#39;DB_HOST&#39;               =>  &#39;localhost&#39;, // 服务器地址
    &#39;DB_NAME&#39;               =>  &#39;km&#39;,       //<span style="font-family: Arial, Helvetica, sans-serif;">数据库名</span>
&#39;,          // 数据库名
    &#39;DB_USER&#39;               =>  &#39;root&#39;,      // 用户名
    &#39;DB_PWD&#39;                =>  &#39;&#39;,          // 密码
    &#39;DB_PORT&#39;               =>  &#39;3306&#39;,        // 端口
    &#39;DB_PREFIX&#39;             =>  &#39;sw_&#39;,    // 数据库表前缀
    &#39;DB_FIELDTYPE_CHECK&#39;    =>  false,       // 是否进行字段类型检查
    &#39;DB_FIELDS_CACHE&#39;       =>  true,        // 启用字段缓存
    &#39;DB_CHARSET&#39;            =>  &#39;utf8&#39;,      // 数据库编码默认采用utf8
);
?>
Copy after login

This is the configuration of the database

&#39;TMPL_ENGINE_TYPE&#39;		=>  &#39;Smarty&#39;,//这是打开smarty模式
Copy after login

3. After connecting to the database, let’s take a look at the core of the tp framework

Nothing to say. It is the mainstream MVC mode used by the tp framework. Open the Home folder and you can see that there are Controller, Model, and View corresponding to the controller, template, and view respectively. I suggest that if you have used your MVC mode and have not learned it well, then you should first Learn MVC well and then learn the tp framework:

namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
 public function index(){
    	$Index = D(&#39;Index&#39;);
		$info=$Index->select();
		$this->assign(&#39;info&#39;, $info);
		$this->display();
    }
}
Copy after login

As shown in the above code, the table name of the database is sw_Index. You put the indicated prefix sw_ in config.php and instantiate the table D() function. In fact, it is Connect the sw_Index table of the database and then query the results directly into the $info "array" and

 {foreach $info as $k => $v}
{$v.xxxx}
{/foreach}
Copy after login

This will loop out all the data in the xxxx field.

That’s it for this introduction. Please criticize if it’s not well written.

Summary: Although the tp framework is troublesome to configure and apply at the beginning, once the template is created, the work efficiency is very fast.

Related recommendations: The latest 10 thinkphp video tutorials

The above is the detailed content of Teach you how to understand the thinkphp framework from scratch?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
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