thinkphp Learning 2#-The first thinkphp example ____php

Source: Internet
Author: User

Test environment: Windows XP SP3

XAMPP 1.7.4 (Apache 2.2.17, MySql 5.5.8, PHP 5.3.5)

Thinkphp 2.1

PHPEclipse

The general development process for creating applications using thinkphp is: Create a database and datasheet (no database operations can be skipped) project name and create a project portal file; complete the project configuration; (no extra configuration can be ignored) create a controller class; Create a model class; (if it's just a simple model class that you don't have to create) Create template files, run and Debug.

1. Create DATABASE and Datasheet

Database name Demo:

CREATE TABLE ' Think_demo ' (
' id ' int (one) unsigned not NULL auto_increment,
' title ' varchar (255) Not NULL default ',
' Content ' longtext not NULL,
PRIMARY KEY (' id ')
) Engine=myisam DEFAULT Charset=utf8;

2, create the project

Create an application in the same directory as the Apache DocumentRoot (Note: This is different from the test environment), the project name is MyApp, the thinkphp core is copied to MyApp, then the new entry file is created index.php

<?php/* Created on 2011-6-10 * * To change the template for this generated file go to * window-preferences-phpec Lipse-php-code Templates *//define thinkphp Frame Road 徂 define (' Think_path ', './thinkphp/'); Define the project name and Lu Ling define (' app_name ', ' Myapp '); Define (' App_path ', '. '); Load frame entry file require (Think_path.) /thinkphp.php "); Instantiate an application instance of a Web site App::run ();?>

Access to the portal file to automatically generate the project directory.

http://localhost:8081/Myapp/index.php

After success, you can see the Welcome page:

3. Project configuration

Under the auto-generated directory, an empty project configuration file has been created, conf/config.php

Open Add Project configuration information:

<?php return Array (//config item ' => ' config value ' app_debug ' => true,//Open debug mode ' db_type ' => ' mysql ',//database type ' Db_host ' => ' localhost ',//database punk address ' db_name ' => ' demo ',//database name ' Db_user ' => ' root ',//Database username ' db_pwd ' => ',//Database password ' Db_por T ' => ' 3306 ',//Database Port ' db_prefix ' => ' think_ ',//data table prefix);?>

4. Add business logic (increase as an example)

Lib/action, automatically generated IndexAction.class.php (Controller), comment (or delete) The current index method, add a new Insert, index method:

Data write operations Public function insert () {$Demo = new model (' Demo ');/////////////////////////////Create data Object $result = $Demo-&G->create T;add (); Write to the database $this->redirect (' index '); Query data $this->assign (' list ', $list); $this->display () of the size of the template; Output template}

5. Template definition

Tpl/default creates the index directory to hold the template file for the index module, and the example only needs to define the template file for the index operation (insert is background operation):

Index.html

<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>thinkphp demo</title>
<body>
<!--data new form-->
<form method= "POST" action= "__url__/insert" >
Title: <input type= "text" name= "title" ><br/>
Content: <textarea name= "Content" rows= "5" cols= "a" ></textarea><br/>
<input type= "Submit" value= "New Data" >
</form>

<!--very loop output inquiry data set-->
<volist name= ' list ' id= ' Vo ' >
Item No: {$vo. ID}
<br/>
Title: {$vo. title}
<br/>
Content: {$vo. Content}
</volist>
</body>

6, testing

Run httP://localhost:8081/Myapp/

Because debug mode is turned on in config, you can see trace information.

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.