thinkphpI. Introduction to PHP Framework Basics
Real project Development Steps:
- Many people at the same time develop projects, collaborative development projects, reasonable division of labor, Efficiency has improved (code style is different, division of labor is not good)
- Testing phase
- On-line operation
- Maintenance, modification, and upgrade of the project (individual maintenance project, very difficult, code style is not the same)
- Stable operation phase of the project
- Project stopped running (the old project staff have all left, the new development project)
Problem:
1. Multi-person development project, the Division of labor is unreasonable, (HTML PHP mysql)
2. Code style is not the same, late maintenance is very difficult
3. Project life cycle is very short, project life is not continuity, resulting in waste of resources, personnel waste
4. The project does not adapt well to customer needs, reaching.
Other relevant frameworks
1. Zendframework Zend PHP Language Company released the official framework, heavyweight (multi-function)
2. Yii American Chinese Development Framework, Xue Qiang, Qiang, heavyweight framework, pure OOP framework
3. CI CodeIgniter Lightweight framework, fast development, flexible deployment
- CakePHP foreign frame, heavyweight, slow
- Symfony Foreign Heavyweight Framework
- Thinkphp lightweight frame, Chinese frame, easy to get started
What frame:
A bunch of code collection, inside there are variables, functions, classes, constants, there are many design patterns MVC, AR database, Singleton and so on.
The framework can save us the 50-60% of our work, and all of our energies are focused on the business level.
Why use Frames
L frame can help us set up the program system quickly, stably and efficiently.
The system has been enhanced by the use of the framework to maximize its maintainability, flexibility and adaptability to customer needs.
The process of using the framework allows us to focus all of our attention on the business level without having to care about the underlying architecture of the program.
thinkphp Version Introduction
thinkphp.cn
thinkphp version 3.2
1. Unzip the files in the Thinkphp folder and change the entry file to index.php
index.php-----is the first PHP file extracted, is a project entry file, that is, all the pages of the project to go this page
2. conf Configuration folder
3. In the Library core resource pool
4. Core document Think in Library core resource pool
Thinkphp-----Core folder (do not modify the contents)
The thinkphp contains:
**common----Core library (storing user-defined functions)
**conf-----configuration file (mainly stored in convention.php)
Home----
Lang----Language Pack
**library-----Core Repository (some core classes are stored: Library/think are all classes)
Mode----Model Folder
TPL-----Templates
Application-----folder to store items, you can change the
Public----Store some common things
Framework Project Deployment
1. Open the index.php portal file and change the original application to Jiaowu (shown below)
<?php//Application Portal File//Detect PHP Environment if (Version_compare (php_version, ' 5.3.0 ', ' < ') die (' Require PHP > 5.3.0! '); /Open debug mode recommend the development phase to open the deployment phase note or set to Falsedefine (' App_debug ', True);//define the application directory define (' App_path ', './jiaowu/');//Your own file name ( Can be casually named here)//Introduce thinkphp entry file require './thinkphp/thinkphp.php ';//pro ^_^ don't need any code, it's that simple.
2. Access to the portal file, the system will automatically create the corresponding application directory file Jiaowu
3. Open the Home folder
MVC pattern
MVC pattern:
Model model models (data) layer
View View Layer
Controller Logic Layer
M: Operational data (data connection, data access)
V: Provide display template
C: Implementing Business logic
MVC: Conventions are better than configurations (conventions are very important)
Controller Access and Routing resolution
Locate the specified controller through the URL address get parameter and make the corresponding method call request
HTTP//URL/index.php?m= module name &c= Controller &a= method
The above URL address information code is not elegant, not secure.
Action: The method that is placed inside the controller
Previous access: Access to specific pages
Access by: Access the operation under the Controller (a method), not a specific page
4 Ways to access 1.MVC
1. Locate the entry file index.php
2. Find the module name, controller, method name (operation)
The following is a LoginController.class.php in the home module that uses 4 ways to access it
<?phpnamespace home\controller;use think\controller;class Logincontroller extends controller{public function Index () { echo "Login---login"; } }
1. http://URL/index.php?m=xx&c=xx&a=xx The basic get mode, localhost:8080/tp/index.php/?m=home&c=login&a= Index
2.http://URL/index.php/module/controller/operation method path mode PathInfo (recommended) notation: Locahost:8080/tp/index.php/home/login/index
3.http://URL/module/controller/operation method rewrite rewrite mode: Http://localhost:8080/tp/Home/Login/index (not implemented by himself) 4.http://URL/index.php?s= /module/controller/method Compatibility mode notation: Http://localhost:8080/tp/index.php/?s=Home/Login/index (4 notation) http://localhost:8080/tp/?s=Home/ Login/index (although I can not know why) 1. Specific URL address mode settings (Profile thinkphp/conf/convertion.php)
Url_model = 0/1/2/3 represents four URL address modes respectively
Shop changed to Jiaowu
Config.php is the configuration file for our current project, and we can modify the file to reach the directory of configuration variables.
This file will overwrite the convertion.php configuration variables during system operation.
Include "convertion.php";
Include "config.php"; After the introduction of the file to the first introduction of the file configuration variables to cover out
In other words: Modifying the configuration is in jiaowu/home/config.php rather than in the core repository convertion.php
Our system is compatible with the use of 4 URL address mode
The system will sometimes automatically create a URL address, which will be created using the URL address according to the current schema
Shortcut function u (); Create a URL address
To modify the configuration:
Special Note: Here the control mode is not to control access, is to control the generated
<? phpnamespace Home\controller; use Think\controller; class Indexcontroller extends Controller { Span style= "color: #0000ff;" >public function Login () {
//
echo "Hello"; Echo U ("Home/index/login"); Echo U ("Index/login");
echo U ("Login");
//
output same//three ways to create a path using the U () shortcut method
Note: If it is under the current controller of the current module, you can write only the method
If it is under the other controller of the current module, write the controller and method
If it is under other modules, the module, controller, method name (operation) must be written
To modify configuration parameters:
' Url_model ' = 1, (most commonly used)
Output Result:/tp/index.php/home/index/login.html
' Url_model ' = 0,
Output Result:/tp/index.php?m=home&c=index&a=login
' Url_model ' = 2,
Output Result:/tp/home/index/login.html
' Url_model ' = 3,
output Result:/tp/index.php?s=/home/index/login.html
Development, production model
Development Debug Mode: The system needs to load about 24 files index.php define ("App_debug", true);
Production mode: The system only needs to load very little about 7 files in the portal file
index.php defined in: Define ("App_debug", false);
Save resources by saving a lot of file development, shutting down the system overhead
To display trace information on the page, you need to be in your own configuration file:
Define (' App_debug ', True); When True:
Define (' App_debug ', False); When false:
And it's going to happen.
Controller and corresponding method creation
The controller is the core of the MVC pattern, and TP has an index controller by default:
If you want to create a controller yourself login:
- Create a new controller file under the \jiaowu\home\controller folder LoginController.class.php
Note: File naming rules, named according to the Hump method
In the IndexController.class.php
<? phpnamespace Home\controller; Use Think\controller; class extends Controller { publicfunction index () { $this Display (); } }
In the view Next level index.html
At this point, when the page appears:
Cause: The above two do not correspond to each other, the template file is not created correctly, this time we are going to create a template file
How to create the template correctly:
If we want to access the index method in the index controller, we first create a template folder corresponding to the index controller, which corresponds to the index controller:
In other words, create a new directory in the View Directory index directory and then create a new index.html file inside
Public "-//w3c//dtd XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >class. PHP counterpart)
The display effect is:
Thinkphp Framework Foundation