2011-09-11 14 views
3

私はサブドメインwww.panel.example.comとドメインwww.example.comを持っています。Kohana 3.2ルーティングとサブドメインの問題

私のbootstrap.phpの:

<?php 
Kohana::init(array(
    'base_url' => '/', 
     'index_file' => FALSE, 
)); 

Route::set('panel', '(<controller>(/<action>(/<id>)))', array('subdomain' => 'panel')) 
    ->defaults(array(
     'directory' => 'panel', 
     'controller' => 'panel', 
     'action'  => 'index', 
     'subdomain' => 'panel', 
    )); 
Route::set('default', '(<controller>(/<action>(/<id>)))') 
    ->defaults(array(
     'controller' => 'home', 
     'action'  => 'index', 
    )); 
?> 

私はブラウザ上で住所を書いている:www.panel.example.com私はエラーを持っている:

HTTP_Exception_404 [ 404 ]: The requested URL/was not found on this server. 

マイ構造:

アプリケーション/クラス/コントローラ(ドメインのコントローラ)

アプリケーション/クラス/コントローラ/パネル(conサブドメインのトローラー)

正しく行う方法は?

+0

あなたはCpanelやPleskのようなホスティングソフトウェアを使用していますか? wyによって、 'www.panel.example.com'は' panel.example.com'ではありませんか? – yoda

+0

私はDirectAdminソフトウェアを使ってサブドメインpanel.example.comを作成しました – witek010

+0

'Kohana'には' subdomains'の組み込みメソッドはありませんが、 'bootstrap.php'でuriを読み込んで' subdomain'名'uri'を再ルーティングします。 – yoda

答えて

3

ルートのサブドメインを処理する方法はありません。だから、私の提案は、インターネットを検索から来ている:、その後

list($subdomain) = explode('.', $_SERVER['SERVER_NAME'], 2); 

このサブドメインに基づいてルートのコントローラまたはディレクトリを呼び出します:

これを行うには

一つの方法は、グローバルSERVERからサブドメインを取得しています

Route::set('panel', '(<controller>(/<action>(/<id>)))') 
    ->defaults(array(
    'directory' => $subdomain, 
    'controller' => 'panel', 
    'action'  => 'index', 
)); 

またはサブドメインを処理する際に、より柔軟にラムダ/コールバックルートを使用:http://kohanaframework.org/3.2/guide/kohana/routing#lambdacallback-route-logic

ティsの答えは、異なるサブドメインに異なるテンプレートを使用することに基づいています。kohana v3: using different templates for different subdomains

0

このコードを使用して、サブドメインのルートを設定する必要があるかどうかを確認します。

//Set an array with subdomains and Configs 
$arrDomainsDirectories = array(
    'services'=>array(
     'subdomain'=>'services', 
     'directory'=>'Services', 
     'controller' => 'Home', 
     'action'  => 'index' 
    ), 
    'default'=>array(
     'subdomain'=>NULL, 
     'directory'=>'', 
     'controller' => 'Home', 
     'action'  => 'index' 
    ) 
); 

//Config Route based on SERVER_NAME 
$subdomain = explode('.', $_SERVER['SERVER_NAME'], 2); 

//If Not Subdomain set Default 
if(count($subdomain) <= 1){ 
    $subdomain = 'default'; 
} else { 
    $subdomain = $subdomain[0]; 
} 

$routeConfig = $arrDomainsDirectories[$subdomain]; 

Route::set('default', '(<controller>(/<action>(/<id>)))', array('subdomain'=>$routeConfig['subdomain'])) 
    ->defaults(array(
     'directory' => $routeConfig['directory'], 
     'controller' => $routeConfig['controller'], 
     'action'  => $routeConfig['action'] 
    ));