1.1 The concept of framework
A framework is actually a collection of reusable codes. The code of the framework is the code of the framework architecture, not Business logic code, framework code protects classes, methods, functions, etc. The framework code is combined according to certain rules to form a framework.
1.2 Problems encountered when developing without using a framework
1. There is no unified specification for code writing
2. Project functions cannot be divided well
3. A small local change may affect the overall situation
4. Project upgrades are troublesome
1.3 Benefits of using frameworks
1. Conducive to team unification Code style
2. Concentrate all your energy on business logic, no need to care about the underlying framework
3. Build the program quickly, stably and efficiently
4. Save a lot of code
5. Later maintenance and upgrades are very convenient
1.5 Related frameworks
1. zendframework: zend official release (zend company is a company that maintains and upgrades PHP language), functions Very powerful, it is a heavyweight framework
2. Yii: A heavyweight framework developed by Chinese people. This framework maximizes the reusability of code
3.cakePHP: Foreign frameworks, slow
4.symfony: foreign frameworks
5.CI: (code Igniter), lightweight framework, fast running speed
6. ThinkPHP framework, free, open source, fast, simple object-oriented (the code inside is both object-oriented and process-oriented), formerly known as FCS, changed its name to ThinkPHP on New Year's Day 2007
1.6 ThinkPHP file structure
Download the TP framework from the official website http://www.thinkphp.cn/ and unzip it after the download is complete. ThinkPHP in the first directory is the core code of our framework, similar to the Framework folder
Conf: Configuration folder, used by all projects built on this TP framework
Library: Class library
1.7.1 Library folder under ThinkPHP
Behavior: Framework runtime auxiliary classes
Think: ThinkPHP core code
Vendor: Some third-party plug-ins
1.7.2 Think folder under ThinkPHPLibrary
Several files to note:
Controller.class.php: Basic controller
Model.class.php: Basic model
Think.class .php: Each request must be executed with files
View.class.php: Basic view
1.8 Structure of the framework
Create a new index.php under the site (Entry file), enter in index.php:
define('APP_PATH','./application/'); //Define the project folder, which needs to end with /
require './ThinkPHP/ThinkPHP.php'; //Contains the ThinkPHP.php file
Note: Multiple entry files can be supported in the TP framework (that is to say, multiple projects are supported);
1.8.1 define('APP_PATH','./application/')
Define the project folder. When the page is executed for the first time, if there is no application folder, the application folder will be automatically created. . When the ThinkPHP.php file is executed, the ThinkPHP framework structure will be automatically built for the first time.
1.9 Create a controller
A controller is a class file with the following specifications:
1. Stored in the Controller folder
under the module (platform) folder
2. Class name: Controller, using Pascal nomenclature
3. The class name and the file name have the same name
4. The file name ends with .class.php
5. ThinkPHP uses UTF-8 encoding by default
6. Try to be case-sensitive. There is no problem in Windows, but it will be case-sensitive in Linux
Pay attention to the namespace when creating the controller and the introduction of basic controllers.
1.10 ThinkPHP’s 4 routes
To accurately locate the method, three parameters are required: platform.controller.method. These three parameters.
a) Normal mode:
Syntax: http://url/index.php/m=module&c=controller&a=method
b) pathinofo() Mode:
Syntax: http://url/index.php/module/controller/method
c) Compatibility mode:
Syntax: http://url /index.php?s=/module/controller/method
d) rewrite rewrite mode:
The URL customization function can make the URL simple and hide the truth by rewriting the route. path.
Pseudo-static technology is the rewrite mode.
Required configuration items:
'URL_ROUTER_ON' => true, //Enable routing
'URL_ROUTE_RULES' => array(
'test' => 'home/Goods/test',
), //Routing rules
Redirect passing parameters
1.11 Definition. Call template
1.11.1 Rules
TP framework calling templates is very simple and powerful. It has its own rules
1. Templates are placed in the view directory
2. A controller corresponds to a folder, and a method corresponds to a page
1.11.2 Calling templates
$this->display();
1.11.3 Assigning values to variables in the controller
$this-> assign('name','tom') //The first method
$this->sex='male'; //The second method
1.11.4 in the template Value in
{$name}
1.12 Project Grouping
A project is divided into at least two groups, one frontend and one backend, each group has its own MVC. When the TP framework automatically generates the project structure, it will automatically generate a front-end group.
Create a new Admin folder (backend folder) in the same directory as Home, and create your own MVC in the folder
1.13 System Constants
__SELF__ : Current Requested address
__MODEL__: current module
__CONTROLLER__: current controller
__ACTION__: current method
get_defined_constants(true) displays all constants, true Indicates group display.
Question: The __CONTROLLER__ constant is a PHP constant. We found that writing this constant directly in the template can be parsed. Why can PHP constants be output in HTML templates? Define a constant NAME in PHP, how to output the value of NAME in the template?
Add a string replacement in the templateContentReplace() method of the ContentReplaceBehavior.class.php file
1.14 Display log information at the bottom of the page
'SHOW_PAGE_TRACE' => true //Display log information at the bottom of the page
After configuration, a small green icon will appear in the lower right corner of the page
1.15 TP production and development mode
define('APP_DEBUG ', TRUE); //Development mode
define('APP_DEBUG', false); //Production mode
1.15.1 Development mode
1. Error prompt comparison Friendly
2. Code modifications will show effects immediately
3. Low execution efficiency
1.16.2 Generation mode
1. The error message is relatively vague
2. Many core code files will be made into a cache file (common~runtime.php), so that requests that originally required loading many files now only need to load one file. Saves a lot of opening and closing overhead.