Home >PHP Framework >ThinkPHP >Teach you how to understand the thinkphp framework from scratch?
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.
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 '../ThinkPHP/ThinkPHP.php'; ?>
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( //'配置项'=>'配置值' 'URL_MODEL' => 1, 'SHOW_PAGE_TRACE' =>false, 'TMPL_ENGINE_TYPE' => 'Smarty', 'SESSION_AUTO_START' =>true, 'URL_CASE_INSENSITIVE' => false, 'DB_TYPE' => 'mysql', // 数据库类型 'DB_HOST' => 'localhost', // 服务器地址 'DB_NAME' => 'km', //<span style="font-family: Arial, Helvetica, sans-serif;">数据库名</span> ', // 数据库名 'DB_USER' => 'root', // 用户名 'DB_PWD' => '', // 密码 'DB_PORT' => '3306', // 端口 'DB_PREFIX' => 'sw_', // 数据库表前缀 'DB_FIELDTYPE_CHECK' => false, // 是否进行字段类型检查 'DB_FIELDS_CACHE' => true, // 启用字段缓存 'DB_CHARSET' => 'utf8', // 数据库编码默认采用utf8 ); ?>
This is the configuration of the database
'TMPL_ENGINE_TYPE' => 'Smarty',//这是打开smarty模式
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('Index'); $info=$Index->select(); $this->assign('info', $info); $this->display(); } }
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}
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!