本文由码农网原创翻译,转载请看清文末的转载要求,谢谢合作!
简介
我手上有个用PHP写的网站,类似于个人测试性质的站点,我独立设计,在这过程中犯了所有可能的设计和编程上的错误,同时我也收获了很多,这也促使我去学习PHP和MySQL。每当我学习新的技术或web实践时,都会想到我的网站会从中得益。正如你可以想象到的,它的可读性不强。测试网站往往在代码可读性和可维护性方面表现糟糕,尤其当你仅仅因为某些必要的功能而学习一门编程语言时,而这也正是我一直以来学习PHP的方式。
自从了解了ASP.NET MVC,我便思考如何在这个网站上实践这套MVC模式。我喜欢这整套概念的一切,尤其是路由。但我并不想为此而重构整个网站的现有框架,担心这样一来,大部分现有代码将变成像是异物一样,不受我的控制和理解。网站已经有一个WordPress搭建的博客,通过修改模板让它看起来就像是一个完整的站点,好比在一巨大的墙上画涂鸦一样,这简直是一团糟。
然后呢?刚不久前,我学习了一些REST API的相关知识,这是为日常工作所需而学习的。有没有一个最佳的方式去学习这些呢?我知道有个网站会从中获益。在网上阅读了一些关于在PHP环境应用RESTful API的文章,我了解了.htaccess文件。现在我知道怎样在PHP中搭建路由,因此,也许能在测试网站实施自己设计的小型的、简单的MVC模型。
设想
在PHP中搭建自定义路由的关键在于增加一个.htaccess文件,以下请看
具体实现:
我在网站文件夹目录中新增了一个”API”文件夹,在里面新建一个.htaccess文件,内容是我从Corey Maynard写的一段文本中参考来的。
<ifmodule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?request=$1 [QSA,NC,L] </ifmodule>
长话短说,有了这个配置,所有以”www.yoursite.com/api/”开头或指向不存在的文件或路径的请求都会被重定向到index.php文件。$1变量值表示请求的URL地址,”request”变量名也很好理解,用$_REQUEST[‘request’]命令就可轻易获取。
另外,index.php文件和.htaccess文件在同一访问层级。
可能出现的问题?
事实上,你可能还没启用指定的module。
在WampServer环境下,我需要启用相应module来支持本机运行。一种方法是点击托盘图标进入”Apache”目录,在”Apache modules”列表中选中rewrite_module,然后重启所有服务。另一种方法是编辑httpd.conf文件,取消下面这段配置的注释
#LoadModule rewrite_module modules/mod_rewrite.so
我想接下来要做的事情之一就是给网站做几个自测网页,好让我确定网站的负责人没有改变任何东西。这听起来可能有点偏执和可笑,但又一次,我需要临时增加 “set names latin2;”配置来检视数据库导入的数据。
如果想验证以上配置是否在服务器上启用,你可以使用这段代码:
echo in_array('mod_rewrite', apache_get_modules());
接下来呢?
至此我获得一些重要的知识,让我可以为我的网站搭建新的MVC框架。使用新框架比使用现有解决方案更棒,很大程度上由于这让我在应用MVC模式的过程中获得广泛的知识。由于所有代码都是我个人的工作量,也就没有任何消耗性组件。
继续为我的网站创建REST API。现在我才能实质性地将数据库逻辑从网页界面中抽离出来。
有用的链接
如果你想知道我是怎样找到解决方案,那下面是我从中获得知识的网站 http://coreymaynard.com/blog/creating-a-restful-api-with-php/
以上就介绍了PHP中的自定义路由使用小结,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

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

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.

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

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.

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

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.

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

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


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)
