2011-07-27 21 views
0

私はMVCに行きましたが、どこにも説明されていない問題が発生しました。それは、1つのコントローラから別のコントローラへリダイレクトする方法です。リダイレクトの問題とPHPでMVCを使用したリンク

することは私には、以下の.htaccessファイルを使用しています:それはその方法とidの目を入れた後

RewriteEngine On 

RewriteCond% {REQUEST_FILENAME}!-F 
RewriteCond% {REQUEST_FILENAME}!-D 

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

は直接コントローラを使用します。

これらすべては標準的な方法でそれらにアクセスしますが、コントローラとして直接使用されるienataをさらにページするためのビューを選択するときには役に立ちます。

<a href="next_page_controler"> NEXT_PAGE </ a> 

、次のコントローラにアクセスするが、私はメソッドにアクセスしたい場合、ここで

<a href="next_page_controler/**controler_model**"> NEXT-pAGE_MODEL </ a> 

とを使用する必要があり、我々は二つの問題がありますされているアドレスバーに繰り返しリンク内

  1. をもう一度表示を繰り返す。

    www.site_pat/next_page_controler/next_page_controler/next_page_controler/next_page_controler/next_page_controler/controler_model

  2. ヘッダー(Location: controler_name)を使用してcontroler_modelメソッドとしてリダイレクトしようとすると、何もメッセージを出したり、試してみたいことはありませんが、リダイレクトは機能しません。私はあなた方の多くが、これらは基本的なことであり、私はフレームワークで仕事を始めたすべてで叫んではこれらの基本を理解する必要があることだと思う直面してきたと思う問題を解決する方法

。あなたのhtaccessのと間違って

答えて

1

Theresの何か、何かのようでなければなりません...

RewriteEngine On 

RewriteCond %{REQUEST_FILENAME}!-F 
RewriteCond %{REQUEST_FILENAME}!-D 

RewriteRule ^(.*)$ index.php/$1 [L, QSA] 
+1

@prodigitalsonあなたは私に例を与えます –

+0

私はindex.phpからurlを取得するためにブートストラップファイルを作成しましたか?url = それは仕事ですが、問題は.htpaccessにありません。リンクとリダイレクト。 –

1

UPDATE

@prodigitalsonは、あなたはとても私の例

を与えるcoudスーパーシンプルな例は次のコードのようになります。実際にはフレームワークを使用しているため、実際にはルータからルータを作成したことはありませんでした。おそらく、この機能には必要な機能があります。ルーティングは、あなたがどれくらい復活できるかによってかなり複雑です。私はいくつかの異なるPHPフレームワークが良い例のためにそれをどうやっているのかを見ていただきたいと思います。

// define routes as a regular expression to match 
$routes = array(
    'default' => array(
    'url' => '#^/categories/(?P<category_slug>\w*(/.*)?$#', 
    'controller' => 'CategoryController' 
    ) 
) 
); 

// request - you should probably encapsulate this in a model 
$request = array(); 

// loop through routes and see if we have a match 
foreach($routes as $route){ 
    // on the first match we assign variables to the request and then 
    // stop processing the routes 
    if(preg_match($route['url'], $_SERVER['REQUEST_URI'], $params)){ 
    $request['controller'] = $route['controller']; 
    $request['params'] = $params; 
    $request['uri'] = $_SERVER['REQUEST_URI']; 
    break; 
    } 
} 

// check that we found a match 
if(!empty($request)){ 
    // dispatch the request to the proper controller 
    $controllerClass = $request['controller']; 
    $controller = new $controllerClass(); 

    // because we used named subpatterns int he regex above we can access parameters 
    // inside the CategoryController::execute() with $request['params']['category_slug'] 
    $response = $controller->execute($request); 
} else { 
    // send 404 
} 

// in this example $controller->execute() returns the compiled HTML to render, but 
// but in most systems ive used it returns a View object, or the name of the template to 
// to be rendered - whatever happens here this is where you send your response 

print $response; 
exit(0); 

あなたは.htaccessファイルからあなたのコントローラルーティングソリーを実装するはずの。あなたは静的資産以外のすべての要求をindex.phpに送ることは簡単です。次にPHP側では、URLのパターンに基づいて要求をどこに送るかを決定します。

関連する問題