编程语言
首页 > 编程语言> > php-Kohana 3.2路由和子域问题

php-Kohana 3.2路由和子域问题

作者:互联网

我有子域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.

我的结构:

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

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

怎么做呢?

解决方法:

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

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

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

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

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

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

该答案基于为不同的子域使用不同的模板:kohana v3: using different templates for different subdomains

标签:kohana,subdomain,php,routing
来源: https://codeday.me/bug/20191208/2087763.html