本节内容:
php实现MVC路由
实现如下的url地址:
http://www.sample.com/index.php/ctr/func/arg
文件,index.php:
<?php
$script_uri = @$_SERVER['REQUEST_URI'];
$seg = array_slice(explode('/', $script_uri), 2);
$ctr = array_shift($seg);
$func = array_shift($seg);
$arg = array_shift($seg);
require_once($ctr.'.php');
$func($arg);
dog.php
function wow($arg) {
if(is_array($arg)) {
print_r($arg);
} else {
echo 'dog wow '.$arg;
}
}
function eat($arg) {
if(is_array($arg)) {
print_r($arg);
} else { // www.jb200.com
echo 'dog eat '.$arg;
}
}
cat.php
function wow($arg) {
if(is_array($arg)) {
print_r($arg);
} else {
echo 'cat wow '.$arg;
}
}
function eat($arg) {
if(is_array($arg)) {
print_r($arg);
} else {
echo 'cat eat '.$arg;
}
}
示例:
request:http://www.sample.com/index.php/dog/eat/bone
response:dog eat bone
request:http://www.sample.com/index.php/cat/wow/mimi
response:cat wow mimi