1. Directory Structure:
Where thinkphp subdirectories are the framework core directory
Thinkphp structure:
2. Entry file
The default comes with a portal file located inpublic/index.php
The application directory isapplication,其结构:
indexModule directory structure:
Index为Controller files;
3. Controller:
Find index the controller of the module Index ;
Find index the Controller for the module Index
To change the return value to HelloWorld
Visit :
See Output results!
4. Read the data:
Database:
You need to add the connection information for the database in your app's database configuration file application/database.php as follows:
<?PHPreturn [ ' Type ' = ' mysql ',//Database Type' Hostname ' = ' 127.0.0.1 ',//Server Address' Database ' = ' outengcms ',//Database name' Username ' = ' root ',//User name' Password ' = ' root ',//Password' Hostport ' = ' 3306 ',//Port' DSN ' = ',//Connection DSN' Params ' = [],//Database Connection Parameters' CharSet ' = ' utf8 ',//database encoding is UTF8 by default' Prefix ' = ' think_ ',//database table Prefixes' Debug ' =true,//Database Debug Mode' Deploy ' = 0,//Database Deployment method: 0 Centralized (single server), 1 distributed (Master-slave server)' Rw_separate ' =false,//whether the database reads and writes separate master-slave valid' Master_num ' = 1,//number of master servers for read-write separation' Slave_no ' = ',//Specify the number from the server' Fields_strict ' =true,//whether the field is strictly checked for existence' Resultset_type ' = ' array ',//Data set return type array Arrays Collection collection Objects' Auto_timestamp ' =false,//whether to write timestamp fields automatically' Sql_explain ' =false,//Whether SQL performance analysis is required];
Next, we modify the Controller method to add code to read the data:
<?phpnamespace App\silingling\controller; UseThink\controller;// Usethink\db;classIndexextendscontroller{ Public function_empty ($name) { return $this->fetch ('/public/404 '); } Public functionTianjia ($code= ' '){ if(!captcha_check ($code)) { $this->error (' captcha error ')); }////Echo 111111111;////Add a database 1 Else { $naa=$_post["Naa"]; $tel=$_post["Tel"]; //Message Contact UsDb::table (' think_shenqing ') ->data ([' Naa ' =$naa, ' tel ' = =$tel]) -Insert (); $this->success (' Add success ', ' Index '); } // // }}
After the controller is written, directly modify the template file, with the label to display it;
5.URL Access
ThinkPHPUsing a single entry mode to access the app, all requests to the app are directed to the app's portal file, and the system URL parses the current requested module, controller, and operation from the parameters, and the following is a standard URL access format:
http://serverName/index.php/模块/控制器/操作
The following subdirectories are called modules, and the modules are all lowercase named
The index controller of the applied module is Index defined as follows:
<?phpnamespace App\silingling\controller; UseThink\controller;// Usethink\db;classIndexextendscontroller{ Public function_empty ($name) { return $this->fetch ('/public/404 '); } Public functionindex () {return $this->fetch ('/public/index '); } Public functionindex1 () {$list=db::name (' Auth_rule ')->where (' sort '),Select (); $this->assign (' list ',$list);//Liucheng$list 3= Db::name (' article ')->where (' writer ',Select ();$this->assign (' List3 ',$list 3);//Chaxun$list 211= Db::name (' Haoma ')->where (' Code ' >0)Select ();$this->assign (' list211 ',$list 211); return $this->fetch ('/public/index1 '); }}
If we access the Portal file index directly, because we do not specify a URL, the system accesses the default action method (index) of the default controller (index) under the default module (index),
http://localhost/index.php
http://localhost/index.php/index/index/index
这两个连接等效!
The index controller of the applied module is Index defined as follows:
<?phpnamespace App\lianxi\controller; UseThink\controller; Usethink\db;classIndexextendscontroller{ Public function_empty ($name) { return $this->fetch ('/public/404 '); } Public functionindex () {return' Index '; } Public functionHello$name= ' World '){ return' Hello, '.$name. ‘!‘; } }
If we directly access the entry file, the default is to go to the index method,
If you want to access the controller's Hello method, you need to use the full URL address
The output is:
Because the name parameter is an optional parameter, the connection is thus lost:
Http://localhost/index.php/lianxi/Index/hello/name/xuanxuan
Output:
6.模板渲染输出:
Output the index template under the current module:
-
Specify template output
-
Output the read template under the user module:
-
$this->display (' User:read ');
Specify the encoding and type when you export the template:
-
Represents the Output XML page type (Note: Here you can output Sitemap Sitemap.xml Oh ~)
-
$this->display (' read ', ' Utf-8 ', ' text/xml ');
To summarize, thinkphp template rendering can set the type of encoding and output file types
thphp5.0 Study Notes (i)