Saturday, June 14, 2008

MVC コントローラー URLによる動的にモジュールを呼ぶ

----- test.php
<?php

/*
e.g
test.php/c=user_proile/m=register/

*/

$path_info = explode('/', $_SERVER['PATH_INFO']);
while(list($cnt,$path) = each($path_info)) {
if ($path == "") continue;
list($key,$value) = explode('=', $path);
if ($key =="c") {
$MyClass = $value;
continue;
}
if ($key =="m") {
$MyMethod = $value;
continue;
}

$_GET[$key] = $value;
}

// overloading is also ok, if PHP5.
if ($MyClass != "" && is_file($MyClass.".php")) {
include_once($MyClass.".php");
if (class_exists($MyClass)) {
$MyModel = new $MyClass;
if (method_exists($MyModel,"dispatch")) {
$MyModel->dispatch($MyMethod);
}
}
}


?>
-----user_proile.php

<?php
class user_proile
{
function dispatch($action)
{
echo $action;

}


}
?>

refer to:
2008/03/php-mvc-controller.html

2005/01/pathinfo.html

No comments: