Home > Backend Development > PHP Tutorial > Decrypt the sample code of module and operation mapping of ThinkPHP3.1.2 version

Decrypt the sample code of module and operation mapping of ThinkPHP3.1.2 version

PHP中文网
Release: 2023-02-28 18:14:02
Original
1335 people have browsed it

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',
 )
Copy after login

URL_MODULE_MAP is an array, each array item represents:

'模块映射名'=>'实际模块名'
Copy after login

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
Copy after login

becomes:

http://serverName/index.php/user/index
http://serverName/index.php/blog/index
Copy after login

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'
    ),
 )
Copy after login

URL_ACTION_MAP parameter is a two-dimensional array. Each array item represents:

'实际模块名'=>array(
  '操作映射名1'=>'实际操作名1'
  '操作映射名2'=>'实际操作名2'
  ......
 )
Copy after login

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
Copy after login

(regardless of the module mapping defined previously):

http://serverName/index.php/Member/register
http://serverName/index.php/Info/list
Copy after login

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',
    ),
 )
Copy after login

Then, the original registration address

http://serverName/index.php/Member/add
Copy after login

becomes

http://serverName/index.php/user/register888
Copy after login

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

用户注册
Copy after login

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 .


http://www.bkjia.com/PHPjc/825438.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/825438.htmlTechArticle Template and The operation mapping function is a 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)...


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template