Summary of use of custom routes in PHP

Source: Internet
Author: User
: This article mainly introduces the use of custom routes in PHP. For more information about PHP tutorials, see. This article is translated by the original author of cainnet. please refer to the reprint requirements at the end of this article. thank you for your cooperation!

Introduction

I have a website written in PHP, similar to a personal test site. I designed it independently and made all possible design and programming mistakes in this process, at the same time, I also gained a lot, which prompted me to learn PHP and MySQL. Whenever I learn new technologies or web practices, I think my website will benefit from them. As you can imagine, it is not readable. Testing websites often do poorly in code readability and maintainability, especially when you learn a programming language just for some of the necessary features, and that is exactly the way I have been learning PHP for a long time.

Since learning about ASP. net mvc, I have been thinking about how to practice this MVC pattern on this website. I like everything about this entire concept, especially routing. However, I do not want to rebuild the existing framework of the entire website for this purpose. I am worried that most of the existing code will become a foreign body and will not be controlled or understood by me. The website already has a blog built by WordPress. modifying the template makes it look like a complete website, like graffiti on a huge wall. this is a mess.

What then? Not long ago, I learned some REST API related knowledge, which is required for daily work. Is there an optimal way to learn this? I know a website will benefit from this. I have read some articles on using RESTful APIs in the PHP environment online. I have learned about the. htaccess file. Now I know how to build a route in PHP. Therefore, I may be able to implement a small and simple MVC model designed by myself on the testing website.

Ideas

The key to setting up a custom route in PHP is to add a. htaccess file. for details, see

Specific implementation:

I added an "API" folder in the folder directory of my website and created a new. htaccess file in it. the content is referenced in a piece of text written by Corey Maynard.

 
  RewriteEngine OnRewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule ^(.*)$ index.php?request=$1 [QSA,NC,L]
 

In short, with this configuration, all requests starting with "www.yoursite.com/api/www.yoursite.com" or directed to non-existing files or other files will be redirected to the index.php file. $1 indicates the request URL. The "REQUEST" variable name can be easily obtained by running the $ _ request ['request'] command.

In addition, the index. php file and the. htaccess file are at the same access level.

Possible problems?

In fact, you may not have enabled the specified module.

In the WampServer environment, I need to enable the corresponding module to support local running. One way is to click the tray icon to go to the "Apache" directory, select rewrite_module in the "Apache modules" list, and restart all services. Another method is to edit the httpd. conf file and cancel the following configuration annotations.

#LoadModule rewrite_module modules/mod_rewrite.so

One of the things I want to do next is to make several self-testing webpages for the website so that I can confirm that the website owner has not changed anything. This may sound a bit paranoid and ridiculous, but again, I need to temporarily add the "set names latin2;" configuration to view the imported data in the database.

To verify whether the above configuration is enabled on the server, you can use this code:

echo in_array('mod_rewrite', apache_get_modules());

What about next?

So far, I have gained some important knowledge so that I can build a new MVC framework for my website. It is better to use a new framework than to use an existing solution, because it gives me a wide range of knowledge in the process of applying the MVC pattern. Since all code is my personal workload, there is no consumable component.

Continue to create REST APIs for my website. Now I can extract the database logic from the web page.

Useful links

If you want to know how I find a solution, the following is my website http://coreymaynard.com/blog/creating-a-restful-api-with-php/

The above introduces the use of custom routing in PHP, including the content, hope to be helpful to friends who are interested in PHP tutorials.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.