有人能告诉我如何创建php类的导航动作,我想写这样的浏览器url
假设我想要主页,那么
http://myserver.mydomain.com/index/home动作将是home函数index.php.
我知道我可以使用$_POST
和$_GET
来实现这一点,但我想尝试一下像codeigniter是如何工作的,到目前为止,我尝试过这样的
<?php
class Index {
function __construct()
{
parent::__construct();
}
function home(){
include('test/header.php');
include('test/home.php');
include('test/footer.php');
}
function about(){
include('test/header.php');
include('test/about.php');
include('test/footer.php');
}
}
?>
假设我们有以下重写规则;
RewriteEngine On
RewriteRule ^([^/]*)$ /index.php?module=$1 [L,QSA]
这将重写请求如下;http://example.php/index.php?module=about
→http://example.php/about
现在,让我们看看Router是如何完成的;
- 这是非常基本的
- 可以改进很多
<?php
class Router {
private $strModule; //Holds the module to load (the page)
public function __construct(){}
public function setModule($strModuleName) {
$this->strModule = $strModuleName;
}
public function loadModule() {
if( file_exists('modules/'. $this->strModule .'.php') ) {
include 'modules/'. $this->strModule .'.php';
} else {
'modules/404.php';
}
}
}
现在,我们使用index.php
$objRouter = new Router();
$objRouter->setModule($_GET['module']);
$objRouter->loadModule();
我们的树会是;
- index.php
- modules/
- about.php
- 404.php
当然,这只是一个快速的工作,它可以得到很大的改进。
我希望这对你有帮助。
注意:漂亮的url(。Htaccess重写规则)只是为了养眼。您可以通过不使用重写规则来实现这一点,即使上面提供的代码与