search
HomeBackend DevelopmentPHP Tutorial解密ThinkPHP3.1.2版本之模块和操作映射_PHP

ThinkPHP

模板和操作映射功能是ThinkPHP3.1.2版本支持的对模块和操作设置的映射机制,由于可以通过改变配置动态改变(实际真正改变,并非别名)URL访问地址,加强了应用的安全性,而且,映射机制具有URL不区分大小写访问的特性,对于应用的迁移也有很大的帮助。

因为,普通情况下,如果需要更改URL的模块或者操作访问的话,需要改动的文件较多,容易导致关联性出错。尤其是很多应用需要迁移到新版本的时候,由于模型和控制器改动较多,导致URL地址出现大的调整,通过模块和操作映射功能,就可以很轻松的解决此类问题。

1.模块映射

要定义模块映射,我们只需要在配置文件中定义:

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

URL_MODULE_MAP是一个数组,每个数组项表示:

'模块映射名'=>'实际模块名'

映射名称不区分大小写,所以设置后,URL访问从原来的:

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

变成了:

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

并且原来的访问URL是失效的,这也是和定义路由方式改变URL的区别之一。没有定义映射的模块访问不变。
定义了模块映射后,可以通过MODULE_ALIAS常量读取当前模块的URL名称。

2.操作映射

不仅是模块名称可以映射,操作名称也支持映射,而且是针对模块来设置的,操作映射的定义方式为:

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

URL_ACTION_MAP参数是一个二维数组,每个数组项表示:

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

操作映射名不区分大小写,如上定义后,URL访问从

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

变成了(不考虑前面定义的模块映射):

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

同样,原来的URL地址访问则失效。没有定义映射的操作访问地址不变。
定义了操作映射后,可以通过ACTION_ALIAS常量读取当前操作在URL地址中的操作名。
操作映射和模块映射可以同时定义,没有影响,例如:

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

则,原来的注册地址

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

变成了

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


3.U函数自动支持

可能很多人会担心,在设置了模块和操作映射后,U函数就会出现需要随之改动的情况。其实不需要担心,因为U函数内部已经自动支持了模块和操作映射的情况。
例如,原来在模板文件中使用了

<a href="{:U('Member/add')}">用户注册</a>

无论如何定义Member模块和add操作的映射,U方法的写法始终保持不变,仍然会正确的指向映射后的URL地址。

总结:

模块和操作映射可以用于如下场合:

1、有经常变化URL需要的场合
2、对URL安全性较高的场合
3、需要移植的应用不希望改变URL地址的场合

需要注意的事项:

在使用了模块和操作映射后,对相关URL地址的路由定义可能需要调整。

Statement
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
PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

PHP Email Security: Best Practices for Sending EmailsPHP Email Security: Best Practices for Sending EmailsMay 08, 2025 am 12:16 AM

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment