Simple Thinkphp Quick Start tutorial

Source: Internet
Author: User

Simple Introduction to MVC [model layer M, control layer C, view layer V]

View

A view is the interface on which the user sees and interacts with it.

Model

The model represents enterprise data and business rules.

Controller

The controller accepts user input and calls models and views to fulfill user requirements.

First small modification

Application \ Home \ Controller (Controller literally understands that this is the control layer mvc c)

IndexController. class. php

Modify the content in $ this-> show ();

Or this file

Public function hello (){
Echo 'hello, thinkphp! ';
}

Access

Http: // localhost/thinkphp/Home/Index/hello

Hello ....

The post policy is to continue execution after the file is output.
The frontend corresponds to _ before_hello ()

Public function _ after_hello (){
Echo 'after <br/> ';
    }
 
Hello, thinkphp! After

Create Business module

The/thinkphp/directory directly creates a file named admin. php.

// Check the PHP environment
If (version_compare (PHP_VERSION, '5. 3.0 ',' <') die ('require PHP> 5.3.0! ');
 
Define ('think _ path', './ThinkPHP /');
 
// Enable the debugging mode. We recommend that you enable the annotation of the deployment phase in the development phase or set it to false.
Define ('app _ debug', True );
 
// Define the application directory
Define ('app _ path', './admin /');
Define ('app _ name', 'admin ');
 
 
// Introduce the ThinkPHP entry file
Require './ThinkPHP. Php ';
// It's so simple that no code is needed in the end of ^_^

The program will automatically create the admin folder and store all the required file directories.

Go to the admin control module and find the file IndexController. class. php.

Admin \ Home \ Controller

$ This-> show ('<style type = "text/css"> * {padding: 0; margin: 0;} div {padding: 4px 48px;} body {background: # fff; font-family: ""; color: #333; font-size: 24px} h1 {font-size: 100px; font-weight: normal; margin-bottom: 12px;} p {line-height: 1.8em; font-size: 36px} </style> <div style = "padding: 24px 48px; ">

Shield the above information
Add the last sentence

$ This-> display ();

\ Thinkphp \ admin \ Home \ View \ Index (View layer)
The directory contains index.html.

Easy to write

That's all.

Template assignment

IndexController. class. php

$ This-> assign ('name', 'monthly upgrade ');
$ This-> display ();
\ Thinkphp \ admin \ Home \ View \ Index \ index.html

Me: {$ name}

It is displayed.

Learn about database configuration

Thinkphp \ Application \ Home \ Conf
File config. php

<? Php
Return array (
// 'Config maps '=> 'configuration value'
// Add database configuration information
'Db _ type' => 'mysql', // Database TYPE
'Db _ host' => 'localhost', // server address
'Db _ name' => 'thinkphp', // database NAME
'Db _ user' => 'root', // USER name
'Db _ pwd' => '', // password
'Db _ port' => 3306, // PORT
'Db _ prefix' => 'think _ ', // database table PREFIX
);
 
 
Public function hello (){
Echo 'hello, thinkphp! ';
    }
 
Change
Public function hello (){
 
Echo 'hello, thinkphp! ';
$ Data = M ('user'); // instantiate the Data model
$ This-> data = $ Data-> select ();
// $ This-> display ();
Print_r ($ this-> data );
 }
Create a database table think_user

Run http: // localhost/thinkphp/Home/Index/hello

Hello, thinkphp! Array ([0] => Array ([id] => 1 [name] => ghj [pwd] => 123456) after

Read native SQL

Echo M ("User")-> getLastSql ();

Execute native SQL

It's easy to use native SQL. We don't even need to instantiate any model, for example:
$ Model = new Model (); // instantiate an empty Model

The following method is equivalent

$ Model = D (); or $ Model = M ();
// Perform the following native SQL operations
$ Model-> query ('select * from think_user where status = 1 ');
$ Model-> execute ('update think_user set status = 1 where id = 1 ');
If you instantiate a model, you can still perform native SQL operations without being affected. For example:

$ User = D ('user ');
$ User-> query ('select * from think_user where status = 1 ');
$ User-> execute ('update think_user set status = 1 where id = 1 ');

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.