PHP self write MVC framework URL rewrite how to write
Ask for guidance
Share to: more
------Solution--------------------
Take an open source framework and look at the code, you know?
------Solution--------------------
Apache full locator to a file forwarding ~
------Solution--------------------
The MVC framework is not directly related to URL rewriting
Usually the frames are single-entry, which makes URL rewriting simple.
------Solution--------------------
Download a thinkphp to see the source.
------Solution--------------------
A 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 ' does not exist this way ';
}
Another file route processing
/*
* ******************************************************************************/
Class Prourl {
/**
* URL routing, to pathinfo format
*/
static function parseURL () {
if (Isset ($_server[' path_info ')) {
Get PathInfo
$pathinfo = explode ('/', trim ($_server[' path_info '), "/"));
Get control
$_get[' m '] = (!empty ($pathinfo [0])? $pathinfo [0]: ' index ');
Array_shift ($pathinfo); Move a cell at the beginning of an array to a group
Get action
$_get[' a '] = (!empty ($pathinfo [0])? $pathinfo [0]: ' index ');
Array_shift ($pathinfo); Then move the cells at the beginning of the array to the group
for ($i =0; $i $_get[$pathinfo [$i]]= $pathinfo [$i +1];
}
}else{
$_get["M"]= (!empty ($_get[' m '])? $_get[' m ']: ' index '); The default is the index module
$_get["A"]= (!empty ($_get[' a '))? $_get[' A ']: ' index '); The default 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
Make a new URL
$url =$_server["Script_name"]. " /{$m}/{$a}/". Str_replace (Array (" & "," = "),"/", $query);
Header ("Location:". $url);
}
}
}
}
Buxie!!
------Solution--------------------
I didn't write a frame, it means passing.
------Solution--------------------
1. Write a single-entry way
2. Through the base controller, through the parameters to control the corresponding controller and method name, such as: C for the controller, m for the method
3. Automatically load classes by object-oriented.
4. To use the MVC Development Framework, you can use Smarty as the kernel
5. You need to have a database driver, you can use PDO, and finally do a protected class to inherit from other classes.
6. There are also components and widgets that need to be integrated into the framework for developers to use, and the general widgets to be used for views.