2012-07-19 5 views
1

PHP MVC 웹 응용 프로그램을 작성하고 싶습니다. .htaccess를 MVC를 지원하도록 구성하는 방법은 무엇입니까?

는 지금 내가

RewriteEngine On 

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

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

을 다음과 같이 .htaccess 파일을 생성 URL에 입력 한 모든 그렇게되는 index.php 경로에 노력하고있어하지만 난 어떤 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) 내 생각, 내가 원하는 것을 얻을 것입니다.

이 문제를 해결하는 방법을 알려주십시오.

감사합니다. Kongthap.

Windows XP에서 EasyPHP를 사용하고 있습니다.

+0

나는 잘 모르겠지만,'사용해보십시오/index.php' –

답변

4

Jalpesh 파텔의 대답에 확장하려면 :

귀하의 .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'; 
+0

당신은 나에게, 내가 원하는 것을 감사하자. – Artisan

1

예를 들어, URL : 당신의 .htaccess에서이 규칙 http://example.com/controller/action1/action2/action3

사용 :

<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의 라인 옹 당신이 본대로 규칙이 추가되었습니다 .-

가 복구 얻을 그래서 앞으로 이러한 값을 검색하기 : method_exists 당신이 컨트롤러 클래스 경우 검증 한 후

$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 (와 방법이있는 경우)(). 액션에 대한

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>");   
      } 
관련 문제