A summary of custom routing usage in PHP

Source: Internet
Author: User
Tags add learn php php file php and php and mysql variable

Brief introduction

I have a website written in PHP, similar to the nature of personal testing site, I independently design, in this process made all possible design and programming errors, and I also harvested a lot, which also prompted me to learn PHP and MySQL. Whenever I learn a new technology or web practice, I think my site will benefit from it. As you can imagine, its readability is not strong. Testing a Web site tends to be bad for code readability and maintainability, especially if you're learning a programming language just because of some necessary functionality, and that's exactly how I've been learning PHP.

Since I learned about asp.net mvc, I've been thinking about how to practice this MVC pattern on this site. I like everything about this whole concept, especially routing. But I don't want to refactor the entire Web site's existing framework for this, fearing that most of the existing code will become like a foreign object, not under my control and understanding. The site already has a WordPress built blog, by modifying the template to make it look like a complete site, like painting graffiti on a huge wall, this is a mess.

And then what? Not long ago, I learned about the rest API, which I learned for my daily work. Is there a best way to learn these things? I know that a website will benefit from it. Read some articles on the internet about applying the RESTful API to the PHP environment, and I learned about the. htaccess file. Now that I know how to build a route in PHP, I might be able to implement a small, simple MVC model of my own design on the test site.


The key to building a custom route in PHP is to add a. htaccess file, see below

Specific implementation:

I added an "API" folder to the site folder directory to create a new. htaccess file, which I referenced from a text written by Corey Maynard.

<ifmodule mod_rewrite.c>
Rewriteengine on
Rewriterule ^ (. *) $ index.php?request=$1 [qsa,nc,l]

Long story short, with this configuration, all requests that start with "www.yoursite.com/api/" or point to a nonexistent file or path are redirected to the index.php file. The variable value represents the URL address of the request, and the "request" variable name is well understood and can be easily obtained using the $_request[' request ' command.

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

Problems that may arise?

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

In the wampserver environment, I need to enable the corresponding module to support native run. One way is to click on the tray icon to enter the "Apache" directory, select Rewrite_module in the "Apache modules" list, and then restart all services. Another way is to edit the httpd.conf file and cancel the comment on the configuration below

#LoadModule Rewrite_module modules/mod_rewrite.so

One of the things I want to do next is to do a few self-test pages for the site so I'm sure the person responsible for the site hasn't changed anything. This may sound a bit paranoid and ridiculous, but again, I need to temporarily add "set names latin2;" Configuration to view the data imported by the database.

If you want to verify that the above configuration is enabled on the server, you can use this code:

echo In_array (' Mod_rewrite ', Apache_get_modules ());

What's next?

So I've got some important knowledge that I can build a new MVC framework for my site. Using the new framework is better than using an existing solution, largely because it allows me to gain a wide range of knowledge in the process of applying the MVC pattern. Since all the code is my personal workload, there is no expendable component.

Continue to create the rest API for my site. Now I can actually pull the database logic out of the Web interface.

Useful Links

If you want to know how I found the solution, here is the website where I get the knowledge from http://coreymaynard.com/blog/creating-a-restful-api-with-php/

Translation Links: http://www.codeceo.com/article/custom-routing-in-php.html
Original English: Custom routing in PHP

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.