thinkphp Tutorial _php Framework thinkphp (ii) "url path access and module controller, URL four mode, pathinfo two modes, template-to-controller relationship"

Source: Internet
Author: User

one, URL path access and module controller

  URL Module (controller) Action (METHOD)    

That is, the relationship between the above three URLs: http://127.0.0.1/projectName/index.php/ module / action

1, thinkphp provisions, two points

• first, all main portal files default access to index controller (module)

• second, all controllers default to perform the index method (action)

  Special emphasis, The above two points are independent! That is, "all main portal Files default access to the index controller and execute the default execution of the index method" is inaccurate

  then, http://127.0.0.1/projectName/index.php (even If no module and action is Specified) will be executed in the Index controller index method, It is a matter of course!

2. (custom) controller naming specification

For example, a user management controller (module) UserAction. class. PHP module name Action.class.php, and the first letter of the module name must be capitalized

  

Above is the file name specification, but also note that the class name to be consistent with the file name, that is, UserAction.class.php the class name must be Useraction. Also note that this class must inherit the action class

  

3, only the public method in the module can be accessed, protected and private methods are not accessible

Obviously this is better understood, because essentially there is $useraction=new useraction in the Portal file; $userAction, method name (); the Two-statement

  So what does it mean to define a private method in a controller? The result is necessary, because for one action in a module, it may be because one action is more complex, need to execute more code , or an action security requirement is higher , Then you need to write some code or high-security code into another private method of this module, and then call within the action (method), so that you can simplify the code, so that the code looks more organized, but also more secure!

  

  Another situation is that in a module, there may be a piece of code is the various actions (controllers) common, then this common code can be encapsulated in a private method, and then for other actions within the module call! May be asked, since it is public, then why not write to the project Directory->common directory in a function file, for this, The personal understanding is the project Directory->common the function is all modules common, and here is a module of the various actions common, donuts!

4, ps, in the actual project development process, it is best to map (custom) controller (or model) each name into Chinese (or english) to write to a document, while explaining what these controllers (or Models) are the methods, and the role of these methods, both easy for others to read and easy to review their own!

Ii. Four modes of the URL

thinkphp supported URL patterns include normal mode ,pathinfo mode ,rewrite mode , and compatibility mode , all of which provide routing support. The default is PathInfo mode, which provides the most user friendly experience (ie convenient for people to see) and search engine friendly support (seo, IE convenient search engine check)! 0, 1, 2, 3, respectively, can be modified in the project directory->conf directory->config.php file

1. Normal mode

Http://127.0.0.1/projectName/index.php?m=moduleName&a=actionName&id=1

2. PathInfo mode

Http://127.0.0.1/projectName/index.php/moduleName/actionName/id/1

Note that in PathInfo mode the Get value cannot be used in this format of the normal mode &id=1, i.e. Http://127.0.0.1/projectName/index.php/moduleName/actionName &id=1, because if this is written, the system will mistakenly think that actionname&id=1 whole is an action name!

  in the default pathinfo mode, if the URL is written in normal mode, it will automatically become the format of the PathInfo mode! Then this is not tested in my environment , the amount ...

3. Rewrite mode

Rewrite mode, related to Pseudo-static

You can write the relevant rewrite rules yourself, or you can use the rewrite rules provided by thinkphp directly, or if you use rewrite rules provided by thinkphp;

Http://127.0.0.1/projectName/moduleName/actionName/id/1

Note that before you can turn on rewrite mode, you need to do two things first

first, Open the Web server rewrite module , take Apache as an example, in the Apache home directory->conf directory-httpd.conf file will be as shown in front of the # Removed. IIS, nginx self-referencing Documentation ha

    

• second, Create A. htaccess file in the directory at the portal file peer , and then copy the following to The. htaccess file to save it! As for why to call the name of The. htaccess, because Apache rules! Here's a little bit more on how to create A. XXX format name file under Windows

    

       

Rewrite mode, the URL of pathinfo mode is also recognizable, but if rewrite mode is not turned on, the URL of rewrite mode is not recognized!

  Interested can refer to the URL Rewriting Guide section of the Apache Manual for further understanding URL rewriting

4. Compatibility mode

Http://127.0.0.1/index.php?s=/moduleName/actionName/id/1

When the server does not support PathInfo mode, if all the URLs of the entire project are written in the PathInfo mode, then there will be an error that the path does not exist! This is obvious, the server does not know your url, how to do it right? however, It is too cumbersome to re-change all the URLs in the project to normal mode, so the thinkphp designer has designed the compatibility mode, which means that the compatibility mode is opened in the project (configured in config.php to 3), even if the server does not support PathInfo Mode. can also identify the url! of the pathinfo mode

The two modes of pathinfo

PathInfo itself has two modes, normal mode , intelligent mode (intelligent identification module and Action)

1. Normal mode

  modules and actions must be specified , i.e. http://127.0.0.1/projectName/index.php/m/modulename/a/ actionname/id/1. And since the module and the action are indicated, the order of the action and the module can be Reversed.

• Set

The same is set in the project directory->conf directory->config.php, "url_pathinfo_model" =>1

2. Intelligent mode

Do not specify the module and the action, the default mode , in the "two, the URL of four modes" in accordance with the smart mode! the so-called intelligent identification module and action, that is, the URL immediately following the entrance file in the first parameter as modulename, the second parameter as a actionname, obviously, this time the sequence of actions and modules can not be reversed

• Set

The same is set in the project directory->conf directory->config.php, "url_pathinfo_model" =>2

Note that either normal mode or smart mode, The get pass must be in the form of a key/value , indicating that the key

  the "url_pathinfo_depr" and "-" setting can modify the delimiter (default =/), i.e. http://127.0.0.1/projectName/index.php/ M-modulename-a-actionname-id-1, again, This setting takes effect for both normal mode and smart mode

  "url_html_suffix" + "html" settings can add. HTML suffix, better support seo, ie http://127.0.0.1/projectName/index.php/ m-modulename-a-actionname-id-1. HTML

Iv. the relationship between the template and the controller

first, you need to know that the application template must be placed in the application Directory->tpl Directory->default directory, Note that the default directory is automatically generated when the application initialization, but also in the TPL directory to customize the directory, white is the custom skin, And the default directory is the application of skin, in the custom directory, of course, can also put templates!

  

second, in each skin directory to the Project module to establish the corresponding directory , and in the established directory for the module action to create the appropriate template

  

  

  

  

  

finally, be aware of the action that invokes the template ($this->display ();) occurs in the action of the Module.

1, with the above three points, and then to specifically say $this->display ();

If no value is passed, the template for the current action in the directory for the current module in the default skin directory is called, for example

  

Some of the small partners may have some doubts, useraction this class does not define the display () method ah, How can directly use display () it? well, forget the extends action on the back! yes, display () is a method in the action class!

If the value is passed, then there are several cases, ps, the value of the word, for the template name , only need to write. the name of the preceding suffix of the html, that is, the corresponding action name

• Call other action templates for the current module

$this->display ("other action name of the current module");

• Invoke action templates from other modules

$this->display ("other module Name: Action name"); in fact, strictly speaking, here the module name and action name refers to the default skin directory is actually a directory name and its following file name, it is not necessary to require the action directory has the module and its actions, and only need to be in the TPL directory The default skin directory has a directory called "other module name" and under it there is a template file called "action name"!

• Invoke templates for other skins

$this->display ("skin name @ module Name: Action name");

· invoking a template directly from a path

$this->display ("template path"), The template path can be either a relative path or an absolute path , but note that if you take a relative path, you must be relative to the portal file

In fact, here to illustrate is, in fact, the first three cases, at the bottom of the transformation into a path , and the Skin name, module name, action name is actually refers to the directory and file name

Above only the first parameter of the display () method, and the display () method has two parameters, namely "template encoding" (specify template output encoding), "template format" (specify template output format (text/html or text/xml))!

2, $this->assign ()

$this->assign ("template variable name", "value or variable");

Here to mention the Change template label delimiter , thinkphp self-contained template engine delimiter default is {}, and the default delimiter is the template JS code and inline CSS conflict, so usually in the actual development, will modify the template tag delimiter, modify the application configuration file as follows

  

3. Template Call case

When debug mode is turned on, thinkphp will simulate the case of file names under linux, but it has been tested to be sensitive only to file name case sensitivity, not to folder name capitalization

  

  

  

  

  

At the same time, also verified the above sentence, that is, "in fact, here to illustrate, in fact, the first three cases, at the bottom of the transformation into a path , and the Skin name, module name, action name is actually refers to the directory and file name "

In fact, in the testing process, also found a problem, that is, loading the module , the operation of the action is not case-sensitive, because if it is loading module or execution action error, reported the following error

  

  

thinkphp Tutorial _php Framework thinkphp (ii) "url path access and module controller, URL four mode, pathinfo two modes, template-to-controller relationship"

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.