【问题标题】:Themes outside application应用程序之外的主题
【发布时间】:2010-05-23 12:04:41
【问题描述】:

我阅读了this post,我想使用类似的解决方案,但使用 db。

在我的站点控制器 after():

$theme = $page->get_theme_name(); //Orange
Kohana::set_module_path('themes', Kohana::get_module_path('themes').'/'.$theme);
$this->template = View::factory('layout')

我检查了萤火虫:

fire::log(Kohana::get_module_path('themes')); // D:\tools\xampp\htdocs\kohana\themes/Orange

我确定该路径存在。我直接在'Orange'文件夹'views'文件夹中带有layout.php文件。

但我得到:找不到请求的视图布局

扩展的 Kohana_Core 只是:

public static function get_module_path($module_key) {
return self::$_modules[$module_key];
}

public static function set_module_path($module_key, $path) {
self::$_modules[$module_key] = $path;
}

谁能帮我解决这个问题?

可能是.htaccess的问题:

# Turn on URL rewriting
RewriteEngine On

# Put your installation directory here:
# If your URL is www.example.com/kohana/, use /kohana/
# If your URL is www.example.com/, use /
RewriteBase /kohana/

# Protect application and system files from being viewed
RewriteCond $1 ^(application|system|modules)

# Rewrite to index.php/access_denied/URL
RewriteRule ^(.*)$ / [PT,L]
RewriteRule ^(media) - [PT,L]
RewriteRule ^(themes) - [PT,L]

# Allow these directories and files to be displayed directly:
# - index.php (DO NOT FORGET THIS!)
# - robots.txt
# - favicon.ico
# - Any file inside of the images/, js/, or css/ directories
RewriteCond $1 ^(index\.php|robots\.txt|favicon\.ico|static)

# No rewriting
RewriteRule ^(.*)$ - [PT,L]

# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [PT,L]

有人可以帮忙吗? 我做错了什么?

问候

[编辑]

我的控制器代码:

class Controller_Site extends Controller_Fly {

   public static $meta_names = array('keywords', 'descriptions', 'author');


   public function action_main() {
        $this->m('page')->get_main_page();
   }

   public function action_page($page_title) {
       $this->m('page')->get_by_link($page_title);
   }

   public function after() {
        $page = $this->m('page');
        $metas = '';
        foreach(self::$meta_names as $meta) {
           if (! empty($page->$meta)) {
              $metas .= html::meta($page->$meta, $meta).PHP_EOL;
           }
        }
        $theme = $page->get_theme_name();
        Kohana::set_module_path('themes', Kohana::get_module_path('themes').'/'.$theme);
        $menus = $page->get_menus();
        $this->template = View::factory('layout')
                           ->set('theme', $theme)
                           ->set('metas', $metas)
                           ->set('menus', $menus['content'])
                           ->set('sections', $page->get_sections())
                           ->set_global('page', $page);
        if ($page->header_on) {
            $settings = $this->m('setting');
            $this->template->header = View::factory('/header')
                                              ->set('title', $settings->title)
                                              ->set('subtitle', $settings->subtitle)
                                              ->set('menus', $menus['header']);
        }
        if ($page->sidebar_on) {
            $this->template->sidebar = View::factory('sidebar', array('menus' => $menus['sidebar']));
        }
        if ($page->footer_on) {
            $this->template->footer = View::factory('footer');
        }
        parent::after();
   }

}

和父控制器:

abstract class Controller_Fly extends Controller_Template {

        protected function m($model_name, $id = NULL) {
            if (! isset($this->$model_name)) {
                   $this->$model_name = ORM::factory($model_name, $id);
            }
            return $this->$model_name;
        }

        protected function mf($model_name, $id = NULL) {
            return ORM::factory($model_name, $id);
        }

}

[编辑 2] 以前的帖子链接已失效,链接为: http://forum.kohanaframework.org/comments.php?DiscussionID=5744&page=1#Item_0

【问题讨论】:

    标签: php apache mod-rewrite kohana kohana-3


    【解决方案1】:

    我的猜测是你的控制器有一个__construct() 方法,而你没有调用parent::__construct

    其实我认为对于 kohana V3 __construct 也需要传递 $request 对象如下:

    public function __construct(Request $request)
    {
        parent::__construct($request);
    }
    

    【讨论】:

      【解决方案2】:

      我意识到我需要再次初始化所有模块:

          $theme = $page->get_theme_name();
          Kohana::set_module_path('themes', Kohana::get_module_path('themes').'/'.$theme);
          Kohana::modules(Kohana::get_modules());
      

      现在我没有错误。相反,我得到了白屏死机:

      $this->template is null
      

      :(

      [编辑]

      现在我知道了,我有:

      $this->template = View::factory('layout')
      ->set('theme', $theme)
      ->set('metas', $metas)
      ->set('menus', $menus['content'])
      ->set('sections', $page->get_sections())
      ->set_gobal('page', $page);
      

      当然我忘了set_global 不会返回$this :D

      无论如何,现在一切正常:)

      我还有一个关于效率的问题。我在请求流中第二次调用Kohana::modules()。就效率而言,这有什么大不了的吗?

      问候

      【讨论】:

      • @efficiency,不,这是一种非常有效的方法,经过测试
      猜你喜欢
      • 2018-10-21
      • 2012-03-17
      • 1970-01-01
      • 1970-01-01
      • 2016-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多