Kohana 3.2路由和子域问题

时间:2022-10-13 12:55:15

I have subdomain www.panel.example.com and domain www.example.com.

我有子域名www.panel.example.com和域名www.example.com。

My bootstrap.php:

我的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',
    ));
?>

When I'm writing adress on browser: www.panel.example.com I have an error:

当我在浏览器上写地址时:www.panel.example.com我有一个错误:

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

My structure:

我的结构:

application/classes/controller (controllers of domain)

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

application/classes/controller/panel (controllers of subdomain)

应用程序/类/控制器/面板(子域控制器)

How to do it properly?

怎么做得好?

2 个解决方案

#1


3  

There is no built in way to deal with subdomains in routes. So my suggestion comes from searching the internet:

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

One way to do this is get the subdomain from the SERVER global:

一种方法是从SERVER全局获取子域:

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

Then, call a controller or directory in the route based on this subdomain:

然后,根据此子域调用路径中的控制器或目录:

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

Or use lambda/callback routes for more flexibility when handling the subdomain: http://kohanaframework.org/3.2/guide/kohana/routing#lambdacallback-route-logic

或者在处理子域时使用lambda / callback路由以获得更大的灵活性:http://kohanaframework.org/3.2/guide/kohana/routing#lambdacallback-route-logic

This answer is based on using different templates for different subdomains: kohana v3: using different templates for different subdomains

此答案基于对不同子域使用不同的模板:kohana v3:为不同的子域使用不同的模板

#2


0  

I use this code to check if subdomain route need to be set.

我使用此代码来检查是否需要设置子域路由。

//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']
    ));

#1


3  

There is no built in way to deal with subdomains in routes. So my suggestion comes from searching the internet:

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

One way to do this is get the subdomain from the SERVER global:

一种方法是从SERVER全局获取子域:

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

Then, call a controller or directory in the route based on this subdomain:

然后,根据此子域调用路径中的控制器或目录:

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

Or use lambda/callback routes for more flexibility when handling the subdomain: http://kohanaframework.org/3.2/guide/kohana/routing#lambdacallback-route-logic

或者在处理子域时使用lambda / callback路由以获得更大的灵活性:http://kohanaframework.org/3.2/guide/kohana/routing#lambdacallback-route-logic

This answer is based on using different templates for different subdomains: kohana v3: using different templates for different subdomains

此答案基于对不同子域使用不同的模板:kohana v3:为不同的子域使用不同的模板

#2


0  

I use this code to check if subdomain route need to be set.

我使用此代码来检查是否需要设置子域路由。

//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']
    ));