MVC Framework Routing principle

Source: Internet
Author: User
Tags explode php define

Many PHP frameworks have been used so far, such as: Zendframework,thinkphp,yii,slim. But the principle of the framework has not been studied yet.

Today, first look at the principle of the MVC framework in the middle:

The so-called routing, is the program according to the browser URL to the program distribution processing (jump) function code.

When we use the framework, we will find that the general URL request is this: LOCALHOST/USER/LOGIN/1, in this URL we are not difficult to find: The user is the name of the controller, and login is the current controller method name, 1 is the passed parameter (possibly the user class).

Let's take a look at the route:

The directory structure for creating a general MVC framework is as follows:

App

--application

----controllers

----Models

----views

--library//Introduction to the library or general method

--public

----Config

------config.php

----index.php//import file

----. htaccess

The principle of routing: The program obtains the request parameter on the URL with $_server, obtains the corresponding position parameter, then loads the corresponding controller method and executes.

A simple example:

Add code to index.php in public:

 <?php define (' Propath ', ' D:/xampp/htdocs/app ');   $root = $_server[' script_name ');     $request = $_server[' Request_uri ');      $URI = Array ();     $url = Trim (Str_replace ($root, ", $request), '/');       # if NULL, the access root address if (empty ($url)) {# Default Controller and default method $class = ' index ';   $func = ' index ';         } else {$URI = explode ('/', $url);           # If function is empty, the default access to index if (count ($URI) < 2) {$class = $URI [0];       $func = ' index ';           } else {$class = $URI [0];       $func = $URI [1]; Load the corresponding class file include (Propath. ‘/’ . ' application/controllers/'. $class.     '. php ');     # instantiation $className = Ucfirst ($class); $obj = new $className ();    Call_user_func_array (///See previous article # Call internal function array ($obj, $func), # Pass parameter Array_slice ($URI, 2)); 
<?php        define (' Propath ', ' D:/xampp/htdocs/app ');   $root = $_server[' script_name ');   $request = $_server[' Request_uri ');     $URI = Array ();      $url = Trim (Str_replace ($root, ", $request), '/');     # if NULL, the access root address   if (empty ($url))   {       # Default Controller and default method       $class = ' index ';       $func = ' index ';   }   else  {       $URI = explode ('/', $url);         # If function is empty, the default access to index       if (count ($URI) < 2)       {           $class = $URI [0];           $func = ' index ';       }       else      {           $class = $URI [0];           $func = $URI [1];       }   }   # load the corresponding class file  include (Propath. ‘/’ . ' application/controllers/'. $class. '. php ');     # instantiation   $className = Ucfirst ($class); $obj = new $className ();     Call_user_func_array (   //See previous article    # Call internal function       Array ($obj, $func),        # Pass parameter       array_slice ($ URI, 2)   );   

Next, add a file in the controllers:

index.php

<?php     class index   {        public  function Index ()       {           echo ' I am default controller ';       }       Public  function Hello ()       {           echo ' Hello word! ';       }      Public Function name ($name)       {           echo ' hello '. $name;       }  }       ? >  
<?php     class index   {        public  function Index ()       {           echo ' I am default controller ';       }       public  function Hello ()       {           echo ' Hello word! ';       }      Public Function name ($name)       {           echo ' hello '. $name;       }  }       ? >  

OK. Below we can test.

Browser input: Localhost/index/index page output: I am Default Controller

Browser input: Localhost/index/hello page output: Hello word!

Browser input: Localhost/index/name/jack page output: Hello Jack

This allows for simple routing.

MVC Framework Routing principle

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.