Decrypt the ThinkPHP3.1.2 version of the module and operation map _php instance

Source: Internet
Author: User
Tags constant decrypt

The template and Action mapping feature is a mapping mechanism for module and operation settings supported by the ThinkPHP3.1.2 version. Because you can change the configuration dynamic change (real change, not alias) URL access address, enhance the security of the application, and the mapping mechanism has a URL case-insensitive access characteristics, the application of the migration also has a Big help.

Because, under normal circumstances, if you need to change the URL of the module or operation access, you need to change more files, easy to cause correlation error. In particular, many applications need to migrate to the new version of the time, because the model and controller changes, resulting in a large URL address adjustment, through the module and operation mapping function, it can easily solve such problems.

1. Module mapping

To define a module map, we only need to define it in the configuration file:

' Url_module_map ' =>array (
  ' user '   => ' member ',
  ' blog '   => ' Info ',
 )

Url_module_map is an array in which each array item represents:

' Module map name ' => ' actual module name '

The mapping name is case-insensitive, so after setting the URL access from the original:

Http://serverName/index.php/Member/index
Http://serverName/index.php/Info/index

has become:

Http://serverName/index.php/user/index
Http://serverName/index.php/blog/index

And the original access URL is invalid, which is also one of the differences between the URL and the definition of the routing change. The module access is unchanged without the mapping defined.
After you define a module map, you can read the URL name of the current module through the Module_alias constant.

2. Operation Mapping

Not only is the module name available for mapping, the action name also supports mapping, and is set for the module, the action map is defined by:

' Url_action_map ' =>array (' member
  '  => Array (
    ' register ' => ' Add ',
    ),
  ' Info '   => Array (
    ' list '   => ' index '
    ),
 )

The Url_action_map parameter is a two-dimensional array in which each array item represents:

' Actual module name ' =>array (
  ' Operation mapping name 1 ' => ' actual operation name 1 '
  operation mapping Name 2 ' => ' actual operation name 2 '
  ...
 )

Action map names are case-insensitive, as defined above, URL access from

Http://serverName/index.php/Member/add
Http://serverName/index.php/Info/index

becomes (regardless of the previously defined module mappings):

Http://serverName/index.php/Member/register
Http://serverName/index.php/Info/list

Similarly, the original URL address access is invalid. The Operation access address for which no mapping is defined is unchanged.
After you define an action map, you can read the action name of the current operation in the URL address by using the Action_alias constant.
Action mappings and module mappings can be defined at the same time without impact, for example:

' Url_module_map ' =>array (
  ' user '   => ', '
 ),
 ' Url_action_map ' =>array (
  ' member '  => Array (
    ' register888 ' => ' Add ',
    ),
 )

Then, the original registered address

Http://serverName/index.php/Member/add

into a

http://serverName/index.php/user/register888


3.U function Automatic support

Many people may worry that after you set up the module and the action map, the U function will need to be changed. In fact, there is no need to worry, because the U function has automatically supported the case of module and operation Mapping.
For example, the template file used the

<a href= "{: U (' Member/add ')}" > User registration </a>

In any case, the mapping of the member module and the add operation is defined, and the U method is always the same, and still correctly points to the mapped URL address.

Summarize:

module and action mappings can be used in the following situations:

1, there are frequent changes in the URL needs of the occasion
2, the security of the high URL of the occasion
3, need to transplant applications do not want to change the URL address of the occasion

Things to be aware of:

after the module and action mappings are used, the routing definition for the associated URL address may need to be adjusted.

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.