How to write mvc framework url rewriting by php?-php Tutorial

Source: Internet
Author: User
Php write mvc framework url rewriting and so on.


Reply to discussion (solution)

Some people can give you some guidance on how to use online tutorials.

Can I see the code after loading an open-source framework?

Apache locates a file forwarding task ~

There is no direct relationship between the mvc framework and url rewriting.
Generally, the framework is single-entry, which simplifies url rewriting.

Download thinkphp and check the source code.

One file call
$ M = $ _ GET ['M']? Ucfirst (strtolower ($ _ GET ['M']): 'index ';
$ Mm = $ m;


$ M = $ m. 'action ';

$ Mod = new $ m;

$ Method = $ _ GET ['A']? $ _ GET ['A']: 'index ';

If (method_exists ($ m, $ method )){
$ Mod-> $ method ();
} Else {
Echo 'The method does not exist ';
}



Route processing of another file
/*
**************************************** ***************************************/
Class Prourl {
/**
* Convert URL routing to PATHINFO format
*/
Static function parseUrl (){
If (isset ($ _ SERVER ['path _ info']) {
// Obtain pathinfo
$ Pathinfo = explode ('/', trim ($ _ SERVER ['path _ info'], "/");

// Obtain control
$ _ GET ['M'] = (! Empty ($ pathinfo [0])? $ Pathinfo [0]: 'index ');

Array_shift ($ pathinfo); // removes the elements starting with an array from the array

// Obtain the action
$ _ GET ['A'] = (! Empty ($ pathinfo [0])? $ Pathinfo [0]: 'index ');
Array_shift ($ pathinfo); // removes the elements starting with an array from the array.

For ($ I = 0; $ I
$ _ GET [$ pathinfo [$ I] = $ pathinfo [$ I + 1];
}

} Else {
$ _ GET ["m"] = (! Empty ($ _ GET ['M'])? $ _ GET ['M']: 'index'); // The index module is used by default.
$ _ GET ["a"] = (! Empty ($ _ GET ['A'])? $ _ GET ['A']: 'index'); // The default value is the index action.

If ($ _ SERVER ["QUERY_STRING"]) {
$ M = $ _ GET ["m"];
Unset ($ _ GET ["m"]); // remove m from the array
$ A = $ _ GET ["a"];
Unset ($ _ GET ["a"]); // remove a from the array
$ Query = http_build_query ($ _ GET); // form 0 = foo & 1 = bar & 2 = baz & 3 = boom & cow = milk format
// Create a new URL
$ Url = $ _ SERVER ["SCRIPT_NAME"]. "/{$ m}/{$ }/". str_replace (array ("&", "="), "/", $ query );
Header ("Location:". $ url );
}
}
}


}




Thank you !!

No framework written, indicating passing

1. write a single portal
2. use the base controller and parameters to control the corresponding controller and method names. for example, c is the controller, m is the method.
3. automatically load classes through object-oriented methods.
4. if you want to use the mvc development framework, you can use smarty as the kernel.
5. a database driver is required. you can use PDO to implement protected classes and inherit other classes.
6. In addition, some components and widgets need to be integrated into the framework for developers to use. generally, widgets are used for views.

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.