The route of MVC

Source: Internet
Author: User
First, complete the routing class

1 Create a folder for other classes to store the classes we created and then modify the path that called the class

2 when our URL is Www.xxx.com/index/index we want to change his access path to the index controller and the index method, we actually visit the Www.xxx.com/index.php/index/index

3 First we want to hide index.php, get the parameter part of the path to return the corresponding controller and method name

4 we want to create a. htaccess file in the root directory as follows:

<ifmodule mod_rewrite.c>
Rewriteengine on
Rewritecond%{request_filename}!-f
Rewriterule ^ (. *) $ index.php/$1 [qsa,pt,l]
</IfModule>

5 We can print $_server variables to see if there is the value we want to get, we need is it inside the URL path value, this value area out of the path name is different request_url there are redirect_url variables, When we do not have a parameter variable behind the URL is '/' or no parameter is we give him a judgment if we give him a hint if it's empty, otherwise we define a variable to get the value inside the Request_url and divide the value into our string with the explode () delimiter;

6 Convert the excess part of the URL into get to destroy the used key value above, and then loop for example:

My default controllers and methods are index/index.

The following is the source code

if (Isset ($_server[' Request_uri ')) && $_server[' Request_uri ']!= '/') {
$path = $_server[' Request_uri '];
$patharray = explode ('/', trim ($path, '/'));
if (isset ($patharray [0])) {
$this->controller = $patharray [0];
}
unset ($patharray [0]);
if (Isset ($patharray [1])) {
$this->action = $patharray [1];
Unset ($patharray [1]);
} else {
$this->action = ' index ';
}
$count = count ($patharray) + 2; Definition 2 is because I remove the front index/index and the key starts at 2, so the length is 2.
$i = 2;
while ($i < $count) {
if (Isset ($patharray [$i + 1])) {
$_get[$patharray [$i]] = $patharray [$i + 1];
}
$i = $i + 2;
}
P ($_get);
} else {
$this->controller = ' index ';
$this->action = ' index ';
}

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.