简体   繁体   English

在Kohana中扩展模板控制器

[英]Extending the Template Controller in Kohana

I'm having a bit of confusion in attempting to retroactively create a new base controller for my project. 在为项目追溯创建新的基本控制器时,我有些困惑。 If I'm not mistaken, all I need to do is create a file in application/libraries called MY_baseController.php containing the following: 如果我没记错的话,我要做的就是在application/libraries创建一个名为MY_baseController.php的文件,其中包含以下内容:

class baseController extends Template_Controller
{
  public function __construct()
  {
    parent::__construct();
  }
}

And then rewrite my other controllers to extend baseController instead of Template_Controller : 然后重写我的其他控制器以扩展baseController而不是Template_Controller

class Frontpage_Controller extends Template_Controller

to

class Frontpage_Controller extends baseController

Yet when I do this, accessing the Frontpage_Controller alerts me that: 但是,当我这样做时,访问Frontpage_Controller提醒我:

Class 'baseController' not found... 找不到类“ baseController” ...

What am I missing here? 我在这里想念什么?

After some fiddling, I think the following is my solution... 经过一番摆弄之后,我认为以下是我的解决方案...

Move MY_baseController.php from application/libraries and into application/controllers . MY_baseController.phpapplication/libraries移至application/controllers Rename it to base.php and change the following line: 将其重命名为base.php并更改以下行:

class baseController extends Template_Controller

into

class Base_Controller extends Template_Controller

Now within your Frontpage Controller, extend Base_Controller instead of baseController . 现在在Frontpage Controller中,扩展Base_Controller而不是baseController

Make sure you follow Kohana Conventions to make sure everything auto-loads properly! 确保遵守Kohana约定,以确保所有内容都能正确自动加载! There are similar ones in relation to Models Helpers and Libraries. 关于模型帮助器和库也有类似的内容。

Also if you want to keep your main application controller folder clean I would suggest making a Kohana module just for your application and put all your template and misc extension controllers there to keep them separate from your main controllers. 另外,如果您想保持主应用程序控制器文件夹的清洁,我建议您为应用程序制作一个Kohana模块,然后将所有模板和杂项扩展控制器放在此处,以使其与主控制器分离。

Just don't forget to add the module to your config file! 只是不要忘记将模块添加到您的配置文件中!

I know this is an old question, but I thought I'd put in a word. 我知道这是一个古老的问题,但是我想说一句话。 You just need to remove the MY_ prefix from the file name as you only really need it when extending a class suffixed with _Core in the system folder. 您只需要从文件名中删除MY_前缀即可,因为只有在扩展系统文件夹中带有_Core后缀的类时才真正需要它。 For example, the file for 例如,用于

class Controller extends Controller_Core

would be named MY_Controller.php. 将被命名为MY_Controller.php。

In this case, just naming the file baseController.php and putting it in the libraries folder would work. 在这种情况下,只需命名文件baseController.php并将其放置在librarys文件夹中即可。

No offense, but I had to bang my head on my computer to get it working with Kohana 3.1. 没有冒犯,但我必须在计算机上动脑子才能使其与Kohana 3.1一起使用。 I finally figured out that the syntax to extend Template Controller should be: 我终于发现扩展模板控制器的语法应为:

class Controller_Base extends Controller_Template

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM