thinkphp Overview 2

thinkphp overview, thinkphp project build process, thinkphp project structure, thinkphp configuration, thinkphp controller, thinkphp model, thinkphp view, thinkphp built-in template engine.

thinkphp is a free open source, fast and simple lightweight object-oriented PHP development framework, followed Apache2 open source license issued, in order to simplify web application development and enterprise development and the birth of references.

thinkPHP characteristics, environmental requirements.

official website:

http://thinkphp.cn

Download the svn:
full version:

http://thinkphp.googlecode.com/svn/trunk

Core Version:

http://thinkphp.googlecode.com/svn/trunk/ThinkPHP

thinkphp follow the simple and practical design principles, thinkphp ideological framework and structure of the system introduced in detail:

thinkphp directory structure, automatically catalogs, deployment project directory naming conventions, the project build process.

thinkphp directory structure: The
system directory and project directory

System directory:
the Common Framework contains a number of public documents, the system definitions and conventions configurations.
lang directory language files.
System library directory lib group.
tpl template directory system.
mode frame mode extensions directory.
vendor directory of third-party libraries.

Project directory:
index.php project entry file
common project public directory
lang language pack project directory
conf project configuration directory
lib project base directory
tpl project template directory
runtime project runtime directory

<?php
define('THINK_PATH','ThinkPHP'); // 定义thinkphp框架路径 define('APP_NAME', 'test'); // 定义项目名称 define('APP_PATH', '.'); // 定义项目路径 require(THINK_PATH."/ThinkPHP.php"); // 加载框架入口文件 App::run(); // 实例化一个网站应用实例 ?>

thinkphp directory automatically generated:

common, conf, lang, lib, runtime, tpl;

Project directory:

admin: admin background management project directory
home: home project directory
common: project a public directory, place the project a public function
conf: project configuration directory, place the configuration file
lang: Project language pack directory
lib: project-based directory, usually including the action and model directories
runtime : project runtime directory, including cache, temp, data and log
tpl: project template directory
thinkphp: thinkPHP system directory
admin.php: admin.php website backstage entrance file
index.php: index.php site entrance file

Grouping module:

app: app project directory
common: project public directory
conf: project configuration directory
lang: Project language pack directory
lib: project base directory
runtime: project runtime directory
tpl: Project template directory
public: public website public directory
css: css style file folder
images : picture folder
js: js script folder

thinkphp system directory
index.php file web portal

thinkphp create a project process:

Create a database, data tables, create a project named import documents, project configuration, create control class, to create a model class, create a template file, run the test.

<?php
return array(
 'APP_DEBUG' => true, // 开启调式模式 'DB_TYPE' => 'mysql', // 数据库类型 'DB_HOST' => 'localhost', // 数据库服务器地址 'DB_NAME' => 'db_database', // 数据库名称 'DB_USER' => 'root', // 数据库用户名 'DB_PWD' => 'root', // 数据库命名 'DB_PORT' => '3306', // 数据库端口 'DB_PREFIX' => 'think_', // 数据表前缀 ); ?>
<?php
class IndexAction extends Action{ public function index(){ $db = new Model('user'); // 实例化模型类,参数数据表名称,不包含前缀 $select = $db -> select(); // 查询数据 $this -> assign('select', $select); // 模板变量赋值 $this -> dispaly('index.html'); // 输出模板 } } ?>

Template file:

<body>
<volist name="select" id="user"> id:{$uer.id}<br/> 用户名:{$user.user}<br/> 地址:{$user.address}<hr> </volist> </body>

thinkphp Configuration

When the configuration file thinkphp Framework Program to the underlying file to run.

Convention configuration, configuration item, mode configuration, grouping configuration, module configuration, dynamic operational configuration.

Definition Format returns PHP array way, all configuration files

<?php
return array(
);
?>

Two-dimensional array configuration

<?php
return array(
'APP_DEBUG' => true, 'USER_CONFIG' =>array( 'USER_AUTH' => true, ... ), ); ?>

Debug configuration

配置文件位于 think\common\debug.php

Configuration file storage location, mode configuration file located in the project configuration directory under
the default debug configuration file:
open logging
closed template cache
records sql log
closed field caching
turned on the running time is shown in detail
open pages trace information shows
a strict check the file case

thinkPHP controller

Module type, stored in the lib \ directory under the action:
the controller class must extend the base class action system

Cross-module calls

$User = A("User"); // 实例化UserAction控制器对象
$User -> insert(); // 调用User模块的importUser操作方法
A("User")是一个快捷方法
等效于:
import("@.Action.UserAction"); $User = new UserAction(); 还有比A更好的方法为: R("User","insert"); // 远程调用UserAction控制器的insert操作方法 $User = A("User","Admin"); // 实例化Admin项目的UserAction控制器对象 $User -> insert(); // 调用Admin项目UserAction控制器的insert操作方法 R("User", "insert", "Admin"); // 远程调用admin项目的useraction控制器的insert操作方法
<?php
header("Content-Type:text/html; charset=uft-8"); // 设置页面编码格式
class IndexAction extends Action { public function index() { $db = new Model('user'); // 实例化模型类,参数数据表名称,不包括前缀 $select = $db->select(); // 查询数据 $this -> assign("select", $select); // 模板变量赋值 $this -> dispaly(); // 输出模板 } } ?>
<?php
header("Content-Type:text/html; charset=utf-8"); // 设置页面编码格式
class UserAction extends Action{ public function insert(){ $ins = new Model("user"); // 实例化模型类,传递参数为没有前缀的数据表名称 $ins -> Create(); // 创建数据对象 $result = $ins -> add(); // 写入数据库 $this -> redirect('Index/index', '', 5, '页面跳转中'); // 页面重定向 } } ?>
<?php
header("Content-Type:text/html; charset=utf-8"); // 设置页面编码格式
class IndexAction extends Action{ public function index() { $db = new Model('user'); // 实例化模型类,参数数据表名称,不包含前缀 $select = $db -> select(); // 查询数据 $this->assign('select',$select);//模板变量赋值 $this->display(); // 输出模板 } public function insert(){ $ins = R("User", "insert", "Admin"); // 远程调用admin项目useraction控制器的insert操作方法 $ins->Create(); // 创建数据对象 $result = $ins->add(); // 写入数据库 } } ?>

thinkphp model

It is synonymous with the model operating in accordance with a certain shape.
The main role model is associated logic package database.
Content:
Named model
instantiation model
property access
connection to the database
to create data
consistency operations
curd operation

Examples of the base model classes

$User = new Model('User');
$User -> select();
$User = M('User');
$User -> select();

m default method is instantiated class model, other models need to be instantiated if categories:

$User = M('User', 'CommonModel');
$User = new CommonModel('User');

Examples of user-defined model class

// 定义的模型类放到项目lib\model目录下面
class UserModel extends Model{ public function myfun() { // 。。。 } }

Examples of ways to customize the model class:

$User = new UserModel();
$User->select(); // 进行其他的数据操作
$User = D('User');
$User -> select(); // 进行其他的数据操作

D model-based method can detect automatically, without the presence of the system will throw an exception, and for the example of the model had not duplicate to instantiate.

$User = D('User','Admin'); // 实例化admin项目下面的User模型
$User->select(); 如果启动模块分组功能,还能使用: $User=D('Admin.User');

Examples of the null model class

$Model = new Model();
// $Model = M();
$Model -> query('SELECT *FROM think_user where status=1');
<?php
$User = new Model('User');
$User -> find(1); echo $User -> name; $User -> name = 'ThinkPHP'; ?> 操作方法是通过返回数组的方式: <?php $Type=D('Type'); // 返回的type数据是一个数组 $type= $Type->find(1); echo $type['name']; // 获取type属性的值 $type['name'] = 'ThinkPHP'; // 设置type属性的值 ?>

Connect to the database:
thinkphp built database access abstraction layer to encapsulate different database operation, only using a common operating Db class.

<?php
return array(
'APP_DEBUG' => false, // 关闭调用模式 'DB_TYPE' => 'mysql', // 数据库类型 'DB_HOST' => 'localhost', // 数据库服务器地址 'DB_NAME' => ‘db_database’, // 数据库名称 'DB_USER' => 'root', // 数据库用户名 'DB_PWD' => 'root', // 数据库密码 'DB_PORT' => '3306'; // 数据库端口 'DB_PREFIX' => 'think_', ); ?>

Connect to the database

Use dsn way to pass initialization parameters db class time.

$db_dsn="mysql://root:[email protected]:3306/db_database";//定义dsn
$db = new Db();// 执行类的实例化
$conn=$db->getInstance($db_dsn);// 连接数据库 数组传参 $dsn = array( 'dbms' => 'mysql', 'username' => 'username', 'password' => 'password', 'hostname' => 'localhost', 'hostport' => '3306', 'database' => 'dbname' ); $db = new Db(); $conn = $db->getInstance($dsn); // 连接数据库,返回数据库驱动类

Class which defines the model parameters:

protected $connection = array(
 'dbms' => 'mysql',
 'username' => 'username', 'password' => 'password', 'hostname' => 'localhost', 'hostport' => '3306', 'database' => 'dbname' ); //或者使用下面的方式: protected $connection = "mysql://username:password@localhost:3306/DbName";

Use pdo connected database:

return array(
 'DB_TYPE' => 'pdo',
 'DB_DSN'=>'mysql:host=localhost;dbname=db_database', 'DB_USER'=>'root', 'DB_PWD'=>'root', 'DB_PREFIX'=>'think_', 'APP_DEBUG' => false, // 关闭调试模式 );

Automatically create data objects based on form data

class UserAction extends Action { // 定义类,继承基础类 public function insert() { // 定义方法 $ins = new Model('user'); // 实例化模型类 $ins -> Create(); // 创建数据库 $result = $ins -> add(); // 写入数据库 $this-> redirect('Index/index', '', 5, '页面跳转中'); // 页面重定向 } }

curd operation

thinkphp provides a flexible and convenient methods of database operation, curd create, update, read and delete.

$User = M("User"); // 实例化对象
$data['name'] = 'ThinkPHP'; $data['email'] = '[email protected]'; $User -> add($data); $User->data($data)->add();

Data reading method:

Read field value using getField method
to read data using the find method
to read the data set using the select method

getField method of reading a field value of

$User = M('User');
$nickname = $User->where('id=3') -> getFielde(‘nickname’); $list = $User->getField('id, nickname');

The return value select method is a two-dimensional array, if there is no query to the outcome, it returns an empty array

$User=M('User');
$list = $User->where('status=1') -> order('create_time') -> limit(10) -> select();

find () method

$User = M("User");
$User->where('status=1 and name="think" ')->find();
<?php
header("Content-Type: text/html; charset=utf-8"); // 设置页面编码格式
class IndexAction extends Action{ public function index(){ $db=M('User'); $select = $db->where('user="mr"')->order('id desc')->limit(3)->select(); $this->assign('select',$select); // 模板变量赋值 $this->display(); // 指定模板页 } public function insert() { $dba = M('User'); $data['user'] = 'fs'; $data['pass'] = md5('gdsoft'); $data['address'] = 'dashu'; $result = $dba -> add($data); if($result) { $this->redirect('Index/index', '', 2, '页面跳转中');//页面重定向 } } ?>
$User = M("User");
$data['name'] = 'ThinkPHP'; $data['email']='[email protected]'; $User -> where('id=5') -> save('data');
$User = M('User');
$User -> where('id=5') -> delete();  $User = M('User'); $User -> where('status=0') ->order('create_time')->limit('5')->delete();

Features thinkphp framework

image.png

What is mvc?

mvc is a classic design program, divided into three parts:
model layer, view layer, control layer.

What is the model layer?
Model layer is the core part of the application, the object may be an entity or a service logic.

View layer provides interface between the application and the user.

The control layer for controlling a program request.

What is CURD?

C is created, U update, R to read, D for deletion.

thinkphp use the add (), save (), select (), and delete ()

What is the single entry?

Automatically generated project directory:

<?php
define('THINK_PATH','ThinkPHP'); // 定义ThinkPHP框架路径 define('APP_NAME', '1'); // 定义项目名称 define('APP_PATH', '.'); // 定义项目路径 require(THINK_PATH."/ThinkPHP.php"); // 加载框架入口文件 App::run(); // 实例化一个网站应用实例 ?>

Project Flow:

<?php
define('THINK_PATH', '../ThinkPHP/'); // 定义ThinkPHP框架路径 define('APP_NAME', '2'); // 定义项目名称和路径 define('APP_PATH', '.'); // 定义项目名称和路径 require(THINK_PATH."/ThinkPHP.php"); // 加载框架入口文件 App::run(); // 实例化一个网站应用实例 ?>

config.php

<?php 
return array(
    'APP_DEBUG' => true, // 开启调试模式 'DB_TYPE'=> 'mysql', // 数据库类型 'DB_HOST'=> 'localhost', // 数据库服务器地址 'DB_NAME'=>'db_database', // 数据库名称 'DB_USER'=>'root', // 数据库用户名 'DB_PWD'=>'rot', // 数据库密码 'DB_PORT'=>'3306', // 数据库端口 'DB_PREFIX'=>'think_', // 数据表前缀 ); ?>
<?php
class IndexAction extends Action{ public function index() { $db = new Model('user'); // 实例化模型类,参数数据表名称,不包含前缀 $select = $db->select(); // 查询数据 $this->assign('select',$select); // 模板变量赋值 $this->display(); // 输出模板 } } ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>ThinkPHP开发流程</title> </head> <body> <!--循环输出查询结果数据集--> <volist name='select' id='user' > ID:{$user.id}<br/> 用户名:{$user.user}<br/> 地址:{$user.address}<hr> </volist> </body> </html>
<?php
define('THINK_PATH', '../ThinkPHP');        //定义ThinkPHP框架路径(相对于入口文件) define('APP_NAME', '3'); //定义项目名称 define('APP_PATH', '.'); //定义项目路径 require(THINK_PATH."/ThinkPHP.php"); //加载框架入口文件 App::run(); //实例化一个网站应用实例 ?>

smarty template technology

What is smarty, characteristics, method of installation and configuration templates, design methods

<?php
    include_once("../config.php");
    $arr = array('computerbook','name' => 'PHP','unit_price' => array('price' => '¥65.00','unit' => '本')); $smarty->assign('title','使用Smarty读取数组'); $smarty->assign('arr',$arr); $smarty->display('02/index.html'); ?>
<?php
    include '../config.php';
    $smarty->assign('title','Smarty保留变量'); $smarty->display('03/index.html'); ?>
<?php
    include_once '../config.php';
    $smarty->display('04/index.html'); ?>
<?php
    include_once "../config.php";
    $smarty->assign("title","if条件判断语句"); $smarty->display("06/index.html"); ?>

 

Guess you like

Origin www.cnblogs.com/daofaziran/p/11571981.html