1

我想编写 PHP MVC Web 应用程序。

现在我正在尝试将任何输入的 URL 路由到 index.php 所以我创建了一个 .htaccess 文件,如下所示

RewriteEngine On

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

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

但是当我尝试输入任何 URL 时,它会将我路由到输入完整路径的 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) 我想,我会得到我想要的。

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

谢谢大家。孔塔。

我在 Windows XP 上使用 EasyPHP。

4

2 回答 2

4

要扩展 Jalpesh Patel 的答案:

您的 .htaccess 会将 url 路径传递给路由器或对示例 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

然后在 index 中,希望将此请求路由到脚本的一部分,该部分执行以下操作:

/*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';
于 2012-07-19T07:04:09.830 回答
1

示例 URL: 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__ );           
        }

为您的行动:$action1

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>" );           
            }
于 2014-03-27T20:03:57.240 回答