本文由码农网原创翻译,转载请看清文末的转载要求,谢谢合作!
简介
我手上有个用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教程有兴趣的朋友有所帮助。

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP remains a powerful and widely used tool in modern programming, especially in the field of web development. 1) PHP is easy to use and seamlessly integrated with databases, and is the first choice for many developers. 2) It supports dynamic content generation and object-oriented programming, suitable for quickly creating and maintaining websites. 3) PHP's performance can be improved by caching and optimizing database queries, and its extensive community and rich ecosystem make it still important in today's technology stack.

In PHP, weak references are implemented through the WeakReference class and will not prevent the garbage collector from reclaiming objects. Weak references are suitable for scenarios such as caching systems and event listeners. It should be noted that it cannot guarantee the survival of objects and that garbage collection may be delayed.

The \_\_invoke method allows objects to be called like functions. 1. Define the \_\_invoke method so that the object can be called. 2. When using the $obj(...) syntax, PHP will execute the \_\_invoke method. 3. Suitable for scenarios such as logging and calculator, improving code flexibility and readability.


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

AI Hentai Generator
Generate AI Hentai for free.

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.

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.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version
Recommended: Win version, supports code prompts!

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