Using the parameters of automatically loading and parsing URLs, the implementation calls to different controllers, implementing the PathInfo mode and the normal URL pattern
File structure:
|--controller
|--index
|--index.php
|--application.php
application.php
<?php class application{public static function main () {header ("content-type:text/html;charset=utf-8");
Self::register ();
Self::router ();
public static function register () {spl_autoload_register ("Self::loadclass");
The public static function LoadClass ($class) {$class =str_replace (' \ \ ', '/', $class); $class = "./". $class. ".
PHP ";
Require_once $class; public static function router () {if (Isset ($_server[' path_info ')) {$pathinfo =array_filter (Explode ("/", $_ser
ver[' Path_info '));
For ($i =1 $i <=count ($pathinfo), $i + +) {$key =isset ($pathinfo [$i])? $pathinfo [$i]: '; $value =isset ($pathinfo [$i +1])?
$pathinfo [$i +1]: "";
Switch ($i) {Case 1: $_get[' m ']=ucfirst ($key);
Break
Case 2: $_get[' C ']=ucfirst ($key);
Break
Case 3: $_get[' a ']= $key;
Break Default:if ($i >3) {if ($i%2==0) {$_get[$key]= $value;
}} break; }} $_get[' m ']=!empty ($_get[' m '))?
Ucfirst ($_get[' m ']): ' Index '; $_get[' C ']=!empty ($_get[' C '])?
Ucfirst ($_get[' C ']): ' Index '; $_get[' A ']=!empty ($_get[' a '])?
$_get[' A ']: ' Index ';
$class = "\\controller\\{$_get[' m ']}\\{$_get[' C ']}";
$controller =new $class;
$controller->$_get[' a '] (); } application::main ();
\controller\index\index.php
<?php
namespace Controller\index;
Use Service\user;
Class index{public
function __construct () {
echo construction method <br/>;
}
Public Function Index () {
new User ();
Print_r ($_get);
}
Public Function Login () {
echo ' login () ';
}
}
Effect:
Above this PHP URL's pathinfo mode load different controller's simple realization is small series to share to everybody's content, hoped can give everybody a reference, also hoped that everybody supports the cloud habitat community.