How to Implement php Routing

Source: Internet
Author: User
How does php implement route distribution in the following url format: the app directory is too large, and the App directory is too large. php default controller zookeeper Modle model directory zookeeper preview View directory zookeeper modules Modul... php implements route distribution in the following url format:

App directory
Zookeeper App
┊ ┡ ┈ Config // configuration file directory
Zookeeper zookeeper Controller // Controller directory
Zookeeper has multiple controllers Index. php // default Controller
┊ ┟ ▓ Modle // model directory
Too many preview View // View directory
Too many modules // Module Directory
Zookeeper administrative authority Admin
Zookeeper zookeeper Controller
Too many rows have been written into Index. php
Too many variables have been used Modle
Too many rows have been written into View
Too many memory attached Member
Zookeeper zookeeper Controller
Too many rows have been written into Index. php
Too many variables have been used Modle
Too many rows have been written into View

  1. Http://www.xxx.com/category/list/order/hot/
    Http://www.xxx.com/admin/category/edit/
    Http://www.xxx.com/member/user/id/3/

  2. Http://www.xxx.com/index.php? C = category & a = list & order = hot
    Http://www.xxx.com/index.php? M = admin & c = category & a = edit
    Http://www.xxx.com/index.php? M = member & c = user & id = 3

The first URLs are frontend access, the category is the controller, and the list is the method under the category class.
2nd background, admin is the module name, category is the controller, and edit is the method in the category class

What should I do to implement the above two types of url for Route distribution?

Reply content:

Php implements route distribution in the following url format:

App directory
Zookeeper App
┊ ┡ ┈ Config // configuration file directory
Zookeeper zookeeper Controller // Controller directory
Zookeeper has multiple controllers Index. php // default Controller
┊ ┟ ▓ Modle // model directory
Too many preview View // View directory
Too many modules // Module Directory
Zookeeper administrative authority Admin
Zookeeper zookeeper Controller
Too many rows have been written into Index. php
Too many variables have been used Modle
Too many rows have been written into View
Too many memory attached Member
Zookeeper zookeeper Controller
Too many rows have been written into Index. php
Too many variables have been used Modle
Too many rows have been written into View

  1. Http://www.xxx.com/category/list/order/hot/
    Http://www.xxx.com/admin/category/edit/
    Http://www.xxx.com/member/user/id/3/

  2. Http://www.xxx.com/index.php? C = category & a = list & order = hot
    Http://www.xxx.com/index.php? M = admin & c = category & a = edit
    Http://www.xxx.com/index.php? M = member & c = user & id = 3

The first URLs are frontend access, the category is the controller, and the list is the method under the category class.
2nd background, admin is the module name, category is the controller, and edit is the method in the category class

What should I do to implement the above two types of url for Route distribution?

Php routes must work with the pseudo-static rules of the server to take effect.

The routing in php meansAddress forwardingYou can input parameters in the URL generation function to generate a new address (which is also a pseudo static address)

I want to help you with the route I write. Http://mengkang.net/17.html

If you are too lazy to read it, let's talk about the general idea:

1. apache or nginx on the server writes pseudo static rewrites to the actual address
Second, when PHP generates a URL address, it will replace the url rules (and this rule will be used for routing files ).
Third, ensure that the user's access must be a pseudo static address. When the user accesses a non-pseudo static address$_SERVER['REQUEST_URI']Determine whether the address is a non-pseudo-static address. If not, jump to the pseudo-static address specified in the route table.

Hope to help the landlord

A single routing rule that forwards all request URLs to index. php for processing.
This is a single portal and the simplest.

However, entry-level general routing rules

# GreenCMS Rewrite Rules
  
   
RewriteEngine on RewriteCond % {REQUEST_FILENAME }! -D RewriteCond % {REQUEST_FILENAME }! -F RewriteRule ^ (. *) $ index. php/$1 [QSA, PT, L]
  

We recommend that you use a streamlined Router database as the routing controller handler and hook. You can use hook to easily customize parameter filtering and login check.

(new Router())->error(405, function($message){    header('Location: /hello/world', true, 302);})->get('/hello/:name', function($name){    echo "Hello $name !!!";})->execute();

If the customer does not set pseudo-static, the default URL is 2nd.

If the customer sets the pseudo-static mode, the rewrite rules for Apache and Nginx are generated separately, so that the customer can copy and configure the rules by themselves.
The rule can be like this ..

#Apache .htaccessRewriteEngine on RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f  RewriteCond %{REQUEST_FILENAME} !-d  RewriteRule . /index.php [L,QSA]

In this way, the program only needs to process in 2nd forms$_SERVER['REQUEST_URI']The URL in the program is dynamically generated based on whether to set pseudo-static.

All requests (except static file requests and nonexistent directories or files) are forwarded to index. php.

The user-defined Router check is performed in the Dispatcher first, and the Router binding operation is executed if the Router match. If the rule does not match, perform the regular operation detection and execution.

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.