繁体   English   中英

URL重写-查询字符串

[英]URL Rewriting - Query String

我在MVC应用程序上工作,并且具有以下格式的网址:

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

我正在尝试重写网址,使其显示为:

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';
}


}

?>

在文档根目录或Apache配置文件的.htaccess中使用以下重写规则:

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

您可以在Martin Melin的站点上轻松测试重写规则。

有关更多信息: Apache的mod_rewrite模块

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM