Learn how to write PHP MVC Framework (1)--routing

Source: Internet
Author: User
Tags php web development

When it comes to PHP web development, the development framework provides us with a flexible approach to development, MVC layer separation, business decoupling, etc...

First of all, let's talk about the MVC framework's routing capabilities ...

General single-entry frame routing is the same structure:

Domain/index.php/classname/functionname/var1/var2

The index.php here is called the entry file ... For the server, you only have access to the controller and the methods inside the index.php, and even the values are implemented within the framework based on the PHP level.

Talk was cheap, show you the code!!

First of all, build the following file structure first

Let's try it out, how to access the files inside the controllers ...

Enter the following in the index.php

Print_r ($_server);

Then visit the following address to try it.

Yourdomain/index.php/class/function/var1

Here the author I am using the local environment, I visited the address is localhost/mvc/index.php/class/function/var1

I'm sticking out the 2 most important variables.

[Request_uri] =/mvc/index.php/class/function/var1

[Script_name] =/mvc/index.php

In fact, the basic principle of routing is here:

Through these 2 variables to extract the URL address of class and function, parameters, etc., and then the class include in, through the PHP callback function Call_user_func_array call the corresponding function and pass the corresponding parameters.

The next code, read the code should be more understandable than I write. haha ~ ~

The contents of index.php are as follows

1<?PHP2  3 #Defining Application Paths4 Define(' APPPATH ',Trim(__dir__, '/'))); 5  6 #get the requested address7 $root=$_server[' Script_name ']; 8 $request=$_server[' Request_uri ']; 9  Ten $URI=Array();  One   A #get the address behind index.php - $url=Trim(Str_replace($root, ‘‘,$request), ‘/‘);  -   the #if empty, the root address is accessed - if(Empty($url))   - {   -     #default controller and default method +     $class= ' Index ';  -     $func= ' Welcome ';  + }   A Else  at {   -     $URI=Explode(‘/‘,$url);  -   -     #if function is empty, the index is accessed by default -     if(Count($URI) < 2)   -     {   in         $class=$URI[0];  -         $func= ' Index ';  to     }   +     Else  -     {   the         $class=$URI[0];  *         $func=$URI[1];  $     }  Panax Notoginseng }   -   the   + #load the class in . A include(APPPATH. ‘/‘ . ' application/controllers/'.$class. '. php ');  the   + #instantiation of - $obj=New Ucfirst($class);  $   $ Call_user_func_array(   -     #Calling internal function -     Array($obj,$func), the     #Passing Parameters -     Array_slice($URI, 2)  Wuyi);

Add the following 2 files to the Application/controllers

Index.php used as the default controller

1<?PHP2  3 classIndex4 {  5  6     functionWelcome ()7     {  8         Echo' I am Default controller '; 9     }  Ten   One }   A   -   -?>

hello.php

1<?PHP2 classHello3 {  4      Public functionindex ()5     {  6         Echo' Hello World '; 7     }  8  9      Public functionName$name)  Ten     {   One         Echo' Hello '.$name;  A     }   - }   -   the?>

Test it and see if you can access it. Based on the routing structure above. Let's try.

This access is normal, correctly called the "Hello" class inside the name method, and then pass the parameter Barbery passed ...

Try not to enter function name and see if you can call index by default:

The answer is yes ...

Last one, visit the root address to see

Also correctly mapped to the default controller ...

OK, a simple MVC routing function is complete ...

Learn how to write PHP MVC Framework (1)--routing

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.