2012-06-30 110 views
0

我的MVC应用程序中工作,我有以下格式的URL:URL重写 - 查询字符串

http://127.0.0.1/PDO/PARTIE%20III/index.php?rt=index/connexion 
http://127.0.0.1/PDO/PARTIE%20III/index.php?rt=index/nouveauMsg 

我要求重写URL,以便它读取:

http://127.0.0.1/PDO/PARTIE%20III/index/connexion 
http://127.0.0.1/PDO/PARTIE%20III/index/nouveauMsg 

不任何人有任何建议来帮助我完成这个url_rewrite?

这是路由器类解析URL并加载正确的观点和/或动作

<?php 

class router { 
/* 
* @the registry 
*/ 
private $registry; 

/* 
* @the controller path 
*/ 
private $path; 

private $args = array(); 

public $file; 

public $controller; 

public $action; 

function __construct($registry) { 
    $this->registry = $registry; 
} 

/** 
* 
* @set controller directory path 
* 
* @param string $path 
* 
* @return void 
* 
*/ 
function setPath($path) { 

/*** check if path i sa directory ***/ 
if (is_dir($path) == false) 
{ 
    throw new Exception ('Invalid controller path: `' . $path . '`'); 
} 
/*** set the path ***/ 
$this->path = $path; 
} 


/** 
* 
* @load the controller 
* 
* @access public 
* 
* @return void 
* 
*/ 
public function loader() 
{ 
/*** check the route ***/ 
$this->getController(); 

/*** if the file is not there diaf ***/ 
if (is_readable($this->file) == false) 
{ 
    $this->file = $this->path.'/error404.php'; 
      $this->controller = 'error404'; 
} 

/*** include the controller ***/ 
include $this->file; 

/*** a new controller class instance ***/ 
$class = $this->controller . 'Controller'; 
$controller = new $class($this->registry); 

/*** check if the action is callable ***/ 
if (is_callable(array($controller, $this->action)) == false) 
{ 
    $action = 'index'; 
} 
else 
{ 
    $action = $this->action; 
} 
/*** run the action ***/ 
$controller->$action(); 
} 


/** 
* 
* @get the controller 
* 
* @access private 
* 
* @return void 
* 
*/ 
private function getController() { 

/*** get the route from the url ***/ 
$route = (empty($_GET['rt'])) ? '' : $_GET['rt']; 

if (empty($route)) 
{ 
    $route = 'index'; 
} 
else 
{ 
    /*** get the parts of the route ***/ 
    $parts = explode('/', $route); 
    $this->controller = $parts[0]; 
    if(isset($parts[1])) 
    { 
     $this->action = $parts[1]; 
    } 
} 

if (empty($this->controller)) 
{ 
    $this->controller = 'index'; 
} 

/*** Get action ***/ 
if (empty($this->action)) 
{ 
    $this->action = 'index'; 
} 

/*** set the file path ***/ 
$this->file = $this->path .'/'. $this->controller . 'Controller.php'; 
} 


} 

>

回答

1

使用在你的文档根或Apache的配置文件中的.htaccess这个重写规则?

RewriteEngine On 
RewriteRule ^PDO/PARTIE%20III/(.*)$ PDO/PARTIE%20III/index.php?rt=$1 [L] 

您可以测试在Martin Melin's site容易重写规则。

欲了解更多信息:Apache's mod_rewrite module

+0

感谢您的回复,我明白这一点。但是,我测试过这个规则,并且它不工作。 我错过了什么吗? – Myst3rii0us

+0

我已经更新了这个问题 - 请让我知道这个问题是否有效。 '.htaccess'应该放在你的站点根目录下。 –

+0

Stil不工作:S ...你可以在这里下载示例http://www.phpro.org/downloads/mvc-0.0.4.tar.gz – Myst3rii0us