如何配置.htaccess以支持MVC?

问题描述:

我想编写PHP MVC web应用程序。如何配置.htaccess以支持MVC?

现在我想航线任何URL输入到index.php,所以我创建为遵循

RewriteEngine On 

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 

RewriteRule ^(.*)$ index.php [R,L,NS] 

.htaccess文件但是,当我试着键入任何URL路由是我的网址同类型的完整路径 - >127.0.0.1/mvc/xxx/ 排到 - >http://127.0.0.1/C:/Program%20Files/EasyPHP-12.0/apache/htdocs/mvc/index.php

无需输入完整路径(C:/Program%20Files/EasyPHP-12.0/apache/htdocs)我想,我会得到我想要的东西。

请帮助如何解决这个问题。

谢谢大家。 Kongthap。

我在Windows XP上使用EasyPHP。

+0

我不知道,但尝试使用'/ index.php' – 2012-07-19 06:43:36

要扩大Jalpesh帕特尔的回答是:

你的.htaccess将通过URL路径的路由器或排序,以便一个例子网址:

http://example.com/mvc/controller/action/action2

RewriteEngine on 
RewriteBase /mvc 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 

RewriteRule ^(.*)$ index.php?request=$1 [L,QSA] 

会发送到index.php?request=controller/action/action2

然后,在索引中,希望将此请求发送到执行某些操作的脚本的一部分翁的线路:

/*Split the parts of the request by/*/ 
$request = (isset($_GET['request']) ? explode('/', $_GET['request']) : null); 
//but most likely $request will be passed to your url layer 
$request[0] = 'controller'; 
$request[1] = 'action'; 
$request[2] = 'action2'; 
+0

你只是让我得到我想要的东西,谢谢。 – Artisan 2012-07-19 07:19:30

一个例子网址: http://example.com/controller/action1/action2/action3

利用这个规则,你的.htaccess:

<IfModule mod_rewrite.c>  
RewriteEngine On 
RewriteCond %{REQUEST_URI} !-f 
RewriteCond %{REQUEST_URI} !-d 
RewriteCond %{REQUEST_URI} !-l 
RewriteRule ^([a-zA-Z_-]*)/?([a-zA-Z_-]*)?/?([a-zA-Z0-9_-]*)?/?([a-zA-Z0-9_-]*)$ index.php?controller=$1&action1=$2&action2=$3&action3=$4 [NC,L] 

考虑在中间怎么强调一个词the_word已添加规则,如您所见_-

由来得那么得到恢复检索这些值:在你验证,如果控制器类

$controller = (isset($_GET['controller']) ? $_GET['controller'] : "IndexController"; 
$action1= (isset($_GET['action1']) ? $_GET['action1'] : "IndexAction"; 
$action2= (isset($_GET['action2']) ? $_GET['action2'] : ""; 
$action3= (isset($_GET['action3']) ? $_GET['action3'] : ""; 

,如果有与class_exists(方法),method_exists()。

if(class_exists($controller."Controller", false)) { 
     $controller = $controller."Controller"; 
     $cont = new $controller(); 
     } 
     else { 
     throw new Exception("Class Controller ".$controller." not found in: "__LINE__);   
     } 

为您的操作:$动作1

if(method_exists($cont, $action1) ) {     
$cont->$action1(); 
    } 
else { 
$cont->indexAction();     
//throw new Exception("Not found Action: <b>$action</b> in the controller: <b>$controller</b>");   
      }