Decrypt the module and operation mapping of ThinkPHP version 3.1. 2

  • 2021-07-02 23:37:20
  • OfStack

Template and operation mapping function is a mapping mechanism for module and operation settings supported by ThinkPHP version 3.1. 2. Because URL access address can be dynamically changed (actually changed, not alias) by changing configuration, the security of application is strengthened. Moreover, the mapping mechanism has the characteristic of URL insensitive to case access, which is also of great help to application migration.

Because, under normal circumstances, if you need to change the module or operation access of URL, there are many files that need to be changed, which will easily lead to correlation errors. Especially when many applications need to migrate to the new version, the URL address is greatly adjusted due to many changes in models and controllers. Such problems can be easily solved through module and operation mapping functions.

1. Module mapping

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


'URL_MODULE_MAP'=>array(
  'user'   => 'Member',
  'blog'   => 'Info',
 )

URL_MODULE_MAP is an array, and each array entry represents:


' Module mapping name '=>' Actual module name '

Mapping names are not case sensitive, so when set, URL accesses from the original:


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

Into:


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

And the original access to URL is invalid, which is also the difference between defining routing mode and changing URL. Access to modules without a defined mapping remains unchanged.
Once the module mapping is defined, the URL name of the current module can be read through the MODULE_ALIAS constant.

2. Action mapping

Not only the module name can be mapped, but also the operation name can be mapped, and it is set for the module. The operation mapping is defined as follows:


'URL_ACTION_MAP'=>array(
  'Member'  => array(
    'register' => 'add',
    ),
  'Info'   => array(
    'list'   => 'index'
    ),
 )

The URL_ACTION_MAP parameter is a 2-dimensional array, and each array entry represents:


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

Operation mapping names are case-insensitive, as defined above, and URL accesses from


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

Become (regardless of the module mapping defined earlier):


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

Similarly, the original URL address access is invalid. Access addresses for operations without defined mappings remain unchanged.
Once the operation map is defined, the operation name of the current operation in the URL address can be read through the ACTION_ALIAS constant.
Operation mappings and module mappings can be defined at the same time without impact, for example:


'URL_MODULE_MAP'=>array(
  'user'   => 'Member',
 ),
 'URL_ACTION_MAP'=>array(
  'Member'  => array(
    'register888' => 'add',
    ),
 )

The original registered address


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

Become


' Module mapping name '=>' Actual module name '

0


3. Automatic support for 3. U functions

Many people may worry that after setting up the module and operation mapping, the U function will need to be changed accordingly. There is no need to worry, because the U function already automatically supports module and operation mapping.
For example, in the template file, the


' Module mapping name '=>' Actual module name '

1

No matter how the mapping between the Member module and the add operation is defined, the U method remains the same and will still correctly point to the mapped URL address.

Summary:

Module and operation mappings can be used in the following situations:

1. There are occasions where URL needs to be changed frequently
2. For occasions with high security for URL
3. Applications that need to be migrated do not want to change URL addresses

Matters needing attention:

After the module and operation mappings are used, the routing definitions for the associated URL addresses may need to be adjusted.


Related articles: