2011-09-11 122 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. 

我的结构:

应用程序/类/控制器(域的控制器)

application/classes/controller/panel(con trollers子域)

如何正确地做到这一点?

+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

没有内置的方式来处理路由中的子域。所以我的建议是来自搜索互联网:

的一种方式做,这是获得来自SERVER全局的子域:

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

然后,调用基于此子域的路由控制器或目录:

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

氏回答是基于使用不同子模板的不同模板: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'] 
    ));