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

次に、うまくいけばこのリクエストをスクリプトの一部にルーティングするインデックスで、次の行に沿って何かを実行します。

/*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 がルールに追加されました _-

来てこれらの値を取得するには、recover を取得します。

$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 に答える