Thinkphp study Note 1

Source: Internet
Author: User
Dare not say original, haha ~~ However, I hope you can take some notes on learning TP ..

Thinkphp is a free, open-source, fast, and simple object-oriented lightweight php development framework. It complies with the apache2 open-source Protocol and is born for agile web application development and simplified enterprise application development. It has many outstanding functions and features. After more than three years of development, with the active participation of the community team, it constantly optimizes and improves usability, scalability, and performance, numerous typical cases ensure stable use for commercial and portal-level development.
Thinkphp draws on many excellent foreign frameworks and modes, uses the object-oriented development structure and MVC mode, and uses a single entry mode, integrates struts's action idea with JSP's taglib (TAG Library), Ror's ORM ing and activerecord mode, and encapsulates curd and some common operations, in project configuration, class library import, template engine, query language, automatic verification, view model, project compilation, cache mechanism, Seo support, distributed database, multi-database connection and switching, authentication mechanism and in terms of scalability, the table is unique.

With thinkphp, you can develop and deploy applications more conveniently and quickly. Of course, it is not just an enterprise-level application. Any PHP application development can benefit from thinkphp's simple and fast features. Thinkphp has many original features and advocates Ultimate simplicity. It develops my development philosophy and uses the least code to complete more functions, the purpose is to make Web Application Development simpler and faster. Thinkphp will continue to absorb and integrate better technologies to ensure its freshness and vitality, and provide best practices for Web application development!

In short, I think that the TP (thinkphp abbreviation, which will not be repeated in the future) Framework is an excellent framework that can meet the basic development needs of everyone.
As a note, I have no need to write some nonsense about the MVC hierarchy of TP system features. For his features and introductions, you can go to the official website to take a look, more should be done by yourself...

Start now:
1. First, familiarize yourself with some basic directory structures.
I. System Directory (thinkphp framework directory)
Public portal file of thinkphp. php framework
Common contains some common files, system definitions, system functions, and Convention configurations of the framework.
Lang system language file
Lib system base class library directory
TPL system template directory
Mode framework mode extension directory
Third-party class library directory
2. Application directory (project directory)
Index. php project entry file (you can use another name or place it in another location)
Common Project Public file directory, usually put the project's public functions
Conf project configuration directory, where all configuration files are stored.
Lang project Language Pack directory (optional)
Lib project class library directory, usually including the action and model subdirectories
TPL Project template directory, supporting template themes
Runtime project runtime directory, including cache (template cache), temp (data cache), data (data directory), and logs (Log File) subdirectories
The above is only the default method. The directory name and structure under the project can be redefined. In fact, the project directory does not need to be manually created by developers. After defining the project's entry file, the system will automatically generate all the directory structures required by the project during the first execution (the premise is that the project directory has the write permission, which should be noted in the Linux environment ).
It can be seen that the new directory structure is easier to deploy and configure, because only the runtime directory requires the write permission. In the Linux environment, you can deploy and configure directory permissions more quickly.

2. TP naming rules
As a framework, thinkphp also has its own specifications. Below are the naming rules that thinkphp should follow as much as possible (officially provided ):
All class files are. class. PHP is a suffix (this refers to the class library file used internally by thinkphp, which does not represent the class library file loaded externally). It is named by the hump method and capitalized, such as dbmysql. class. PHP.
Functions, configuration files, and other class library files are generally suffixed with. php (third-party introduction is not required ).
Make sure that the file name is the same as the call case, because it is case sensitive on Unix-like systems (while thinkphp is in the debugging mode, even on Windows platforms, the Case sensitivity is strictly checked ).
The class name and file name are the same (including the case-insensitive). For example, the file name of the useraction class is useraction. Class. php, and the file name of the infomodel class is infomodel. Class. php,
The function name uses lowercase letters and underscores, for example, get_client_ip.
The action controller class is suffixed with action, such as useraction and infoaction.
The model class is suffixed with model, such as usermodel and infomodel.
The method name uses the camper method, and the first letter is lowercase, for example, GetUserName
The attribute name uses the camper method and the first letter is lowercase, for example, tablename.
A function or method with the double underscore "_" as the magic method, such as _ call and _ autoload
Constants are named with uppercase letters and underscores, such as has_one and many_to_prop.
Name the configuration parameter with uppercase letters and underscores (_), for example, html_cache_on.
Language variables are named with uppercase letters and underscores (_). For example, my_lang. Language variables with the following dashes are usually used for system language variables, such as _ class_not_exist _.
Data Tables and fields are named with lowercase underscores, such as think_user and user_name.
Special case:
In thinkphp, there is a special name for a function, that is, a single-letter upper-case function, which is usually a quick definition of some operations or has a special role. For example, the ADSL method has a special meaning and will be known later.
Also, thinkphp uses UTF-8 encoding by default, so please make sure that your program files are saved in UTF-8 encoding format, and remove the BOM information header (remove BOM header information in many ways, different editors have different settings. You can also use tools to perform unified detection and processing ).

3. Start with "Header" -- index. php
Thinkphp uses a single portal mode for project deployment and access. No matter what functions are completed, a project has only one unified portal (but not necessarily the only one. The entry files of all projects are similar. The main functions of the entry files are as follows:
Path definition project name definition (optional)
Additional Parameter definition (optional)
Load the framework entry file (required)
Instantiate an app (required)
The official website provides a standard entry File Syntax:

  1. <? PHP
  2. // Define the thinkphp framework path (relative to the entry file)
  3. Define ('Think _ path', './thinkphp /');
  4. // Define the project name and Path
  5. Define ('app _ name', 'My _ app ');
  6. Define ('app _ path ','./');
  7. // Load the framework entry file
  8. Require think_path. 'thinkphp. php ';
  9. // Instantiate a website application instance
  10. // $ APP = new app ();
  11. // $ App-> Run ();
  12. APP: Run (); // equivalent to the above
  13. ?>

Copy code

Then you can see:
^_^ Hello, welcome to thinkphp's stuff. It indicates that your configuration is successful.
The root directory is changed from the original TP framework directory + index. php to common + conf + Lang + lib + runtime + TPL, which are automatically generated by TP.
I have already mentioned their usage, so I will not talk about it here.

4. How to access it?
The thinkphp framework is accessed through modules and operations. Because thinkphp framework applications use a single entry file for execution, therefore, all modules and operations on the website are accessed and executed through URL parameters. In this way, the traditional file entry access will be resolved and scheduled by URL parameters.
Thinkphp's powerful URL parsing, scheduling, and routing functions provide a powerful guarantee for the implementation of this function, and can be successfully deployed in most server environments.
The URL modes supported by thinkphp include normal mode, pathinfo mode, rewrite mode, and compatibility mode, and provide route support. The default pathinfo mode provides the best user experience and friendly support for search engines.
The above are from official terms ....
The Controller of thinkphp is a module class, which is usually located under the Lib \ action directory of the project. The class name is the module name with the action suffix. For example, the indexaction class indicates the index module.
The method in each module is an action.
In TP, if we want to access the method in the Controller (module), the URL format is as follows:
Http: // localhost/appname/index. php? M = modulename & A = actionname
So, when we enter index. php on the homepage, replace it with index. php? M = index & A = index found that the page has not changed.
However, her address is changed to/index. php/index/. This is because TP uses the pathinfo mode by default.

5. Start our first test instance.
Open indexaction. Class. php of LIB/action.
Add the following code after the index method:

  1. Public Function hfphp (){
  2. Echo "Welcome to 中 -Hefei PHP Training Center ";
  3. }

Copy code

So we can access index. php? M = index & A = hfphp.

I will continue writing in the future. Of course, I hope you can get your support,

Source: http://bbs.hfphp.org/thread-152-1-1.html

At the same time, welcome to visit Anhui PHP community: http://bbs.hfphp.org/

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.