我有子域名www.panel.example.com和域名www.example.com。
我的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(子域控制器)
如何正确地做到这一点?
答案 0 :(得分:3)
没有内置的方法来处理路由中的子域。所以我的建议来自于搜索互联网:
执行此操作的一种方法是从SERVER
global:
list($subdomain) = explode('.', $_SERVER['SERVER_NAME'], 2);
然后,根据此子域调用路径中的控制器或目录:
Route::set('panel', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'directory' => $subdomain,
'controller' => 'panel',
'action' => 'index',
));
或者在处理子域时使用lambda / callback路由以获得更大的灵活性:http://kohanaframework.org/3.2/guide/kohana/routing#lambdacallback-route-logic
此答案基于对不同子域使用不同的模板:kohana v3: using different templates for different subdomains
答案 1 :(得分: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']
));