Home > Article > Backend Development > Decrypt the sample code of module and operation mapping of ThinkPHP3.1.2 version
The template and operation mapping function is the mapping mechanism for modules and operation settings supported by ThinkPHP version 3.1.2. Since it can be changed dynamically by changing the configuration (the actual change is not an alias) URL access address strengthens the security of the application. Moreover, the mapping mechanism has the characteristics of URL case-insensitive access, which is also very helpful for application migration.
Because, under normal circumstances, if you need to change the module of the URL or operate the access, there are many files that need to be changed, which can easily lead to correlation errors. Especially when many applications need to be migrated to a new version, due to the large changes in Model and Controller, the URL address will be greatly adjusted. Through the module and operation mapping function, you can It is easy to solve such problems.
1. Module mapping
To define module mapping, we only need to define in the configuration file :
'URL_MODULE_MAP'=>array( 'user' => 'Member', 'blog' => 'Info', )
URL_MODULE_MAP is an array, each array item represents:
'模块映射名'=>'实际模块名'
The mapping name is not case-sensitive, so after setting, the URL is accessed from the original:
http://serverName/index.php/Member/index http://serverName/index.php/Info/index
becomes:
http://serverName/index.php/user/index http://serverName/index.php/blog/index
and the original access URL is invalid. This is also one of the differences from changing the URL in the defining route method. Module access without a mapping is unchanged.
After defining the module mapping, you can read the URL name of the current module through the MODULE_ALIAS constant .
2. Operation mapping
Not only module names can be mapped, operation names also support mapping, and they are set for modules. The operation mapping is defined as: The
'URL_ACTION_MAP'=>array( 'Member' => array( 'register' => 'add', ), 'Info' => array( 'list' => 'index' ), )
URL_ACTION_MAP parameter is a two-dimensional array. Each array item represents:
'实际模块名'=>array( '操作映射名1'=>'实际操作名1' '操作映射名2'=>'实际操作名2' ...... )
The operation mapping name is not case-sensitive. After being defined as above, URL access has changed from
http://serverName/index.php/Member/add http://serverName/index.php/Info/index
(regardless of the module mapping defined previously):
http://serverName/index.php/Member/register http://serverName/index.php/Info/list
Similarly, the original URL address access will be invalid. The access address of operations without defined mapping remains unchanged.
After defining the operation mapping, you can read the operation name of the current operation in the URL address through the ACTION_ALIAS constant.
Operation mapping and module mapping can be defined at the same time without any impact. For example:
'URL_MODULE_MAP'=>array( 'user' => 'Member', ), 'URL_ACTION_MAP'=>array( 'Member' => array( 'register888' => 'add', ), )
Then, the original registration address
http://serverName/index.php/Member/add
becomes
http://serverName/index.php/user/register888
3. U function automatically supports
Many people may be worried that after setting up the module and operation mapping, the U function will appear and need to be followed Changes to the situation. In fact, there is no need to worry, because the U function has automatically supported module and operation mapping internally.
For example, if
用户注册
is used in the template file, no matter how the mapping between the Member module and the add operation is defined, the writing of the U method will always remain the same and will still correctly point to the mapped URL. address.
Summary:
Module and operation mapping can be used in the following situations:
1. Occasions where URLs need to be changed frequently
2. For occasions with high URL security
3. When the application that needs to be transplanted does not want to change the URL address
Things to note:
When using modules and operations After mapping, the route definition for the relevant URL address may need to be adjusted .