【问题标题】:Kohana 3.2 Routing and subdomains problemKohana 3.2 路由和子域问题
【发布时间】:2011-09-11 16:08:05
【问题描述】:

我有子域 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(域的控制器)

application/classes/controller/panel(子域的控制器)

如何正确操作?

【问题讨论】:

  • 您是否在使用任何托管软件,例如 Cpanel 或 Plesk?顺便说一句,www.panel.example.com 不应该是 panel.example.com 吗?
  • 我使用托管软件 DirectAdmin 并创建了子域 panel.example.com
  • Kohana 没有subdomains 的内置方法,但您可以读取bootstrap.php 上的uri 并捕获subdomain 名称并重新路由uri
  • 这是一个关于我如何使用v3.1.x 的示例:pastebin.com/3Q4YbLWq(代码有点混乱,但想法就在那里,并且奏效了)。请注意,该过程可能会根据您的托管软件创建子域的方式而改变(例如,cPanelpublic_html 中将子域创建为文件夹,而Plesksubdomains\name 外部httpdocs 中创建它们)

标签: php routing kohana subdomain


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多