PHPMVC framework route learning Notes

Source: Internet
Author: User
Tags php web development
The article focuses on the use of PHPMVC framework routing. if you need to know how to develop PHP web, you can't do without the development framework. The Development Framework provides us with flexibility.

This article mainly describes how to use php mvc framework routing. if you need to know about it, please refer to it. speaking of PHP web development, we naturally cannot leave the development framework. The Development Framework provides us with flexible development methods, MVC layer separation, and business decoupling...

The first article is a simple one. let's talk about the routing function of the MVC framework...

Generally, a single-entry framework route entry adopts the following structure:

Domain/index. php/classname/functionname/var1/var2 index. php is called an entry file... for the server, you only access index. php calls the controller and the method in it, and even transmits values based on the PHP layer in the framework.

Talk is cheap, show you the code !!

First, set up the following file structure. let's try it. how can we access the file in controllers... input the following content in index. php:

Print_r ($ _ SERVER );

Visit the following address to try.

Yourdomain/index. php/class/function/var1

Here, I use a local environment. the address I access is localhost/MVC/index. php/class/function/var1.

I posted the two most important variables.

[REQUEST_URI] =>/MVC/index. php/class/function/var1

[SCRIPT_NAME] =>/MVC/index. php

In fact, the most basic principle of routing is here:

These two variables are used to extract the class, function, and parameters in the url address, and then include the class. the callback function call_user_func_array of PHP is used to call the corresponding function and pass the corresponding parameters. next, the code should be easier to read than I write. haha ~~

The index. php instance code is as follows:

  1. # Define the application path
  2. Define ('apppath', trim (_ DIR __,'/'));
  3. # Obtain the request address
  4. $ Root = $ _ SERVER ['script _ name'];
  5. $ Request = $ _ SERVER ['request _ URI '];
  6. $ URI = array ();
  7. # Obtain the address after index. php
  8. $ Url = trim (str_replace ($ root, ", $ request ),'/');
  9. # If it is null, it is the access root address
  10. If (emptyempty ($ url ))
  11. {
  12. # Default controller and default method
  13. $ Class = 'index ';
  14. $ Func = 'Welcome ';
  15. }
  16. Else
  17. {
  18. $ URI = explode ('/', $ url );
  19. # If function is empty, index is accessed by default.
  20. If (count ($ URI) <2)
  21. {
  22. $ Class = $ URI [0];
  23. $ Func = 'index ';
  24. }
  25. Else
  26. {
  27. $ Class = $ URI [0];
  28. $ Func = $ URI [1];
  29. }
  30. }
  31. # Load the class
  32. Include (APPPATH. '/'. 'application/controllers/'. $ class.'. php ');
  33. # Instantiation
  34. $ Obj = new ucfirst ($ class );
  35. Call_user_func_array (
  36. # Calling internal functions
  37. Array ($ obj, $ func ),
  38. # Passing parameters
  39. Array_slice ($ URI, 2)
  40. );

Add the following two files to application/controllers. index. php is used as the default controller.

The instance code is as follows:

  1. Class Index
  2. {
  3. Function welcome ()
  4. {
  5. Echo 'I am default controller ';
  6. }
  7. }
  8. ?>
  9.  
  10. Hello. php
  11.  
  12. Class Hello
  13. {
  14. Public function index ()
  15. {
  16. Echo 'hello world ';
  17. }
  18. Public function name ($ name)
  19. {
  20. Echo 'hello'. $ name;
  21. }
  22. }
  23. ?>

Test to see if you can access the service. according to the above routing structure. let's try to see if the access is normal. the name method inside the class hello is correctly called, and the barbery parameter is passed... try again without entering function name to see if index can be called by default. the answer is also acceptable... finally, access the root address to see if it is correctly mapped to the default controller... OK, a simple MVC routing function is complete...

Related Article

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.