扩展 Kohana 中的模板控制器

发布于 2024-08-02 09:42:44 字数 677 浏览 6 评论 0原文

我在尝试为我的项目追溯创建新的基本控制器时遇到了一些困惑。如果我没记错的话,我需要做的就是在 application/libraries 中创建一个名为 MY_baseController.php 的文件,其中包含以下内容:

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

然后重写我的其他控制器以扩展baseController 而不是 Template_Controller

class Frontpage_Controller extends Template_Controller

然而

class Frontpage_Controller extends baseController

,当我执行此操作时,访问 Frontpage_Controller 会提醒我:

找不到类“baseController”。 ..

我在这里缺少什么?

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:

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

And then rewrite my other controllers to extend baseController instead of 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:

Class 'baseController' not found...

What am I missing here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

巡山小妖精 2024-08-09 09:42:44

经过一番摆弄,我认为以下是我的解决方案...

MY_baseController.phpapplication/libraries 移动到 application/控制器。将其重命名为 base.php 并将以下行更改

class baseController extends Template_Controller

class Base_Controller extends Template_Controller

Now 在 Frontpage 控制器中,扩展 Base_Controller 而不是 baseController

After some fiddling, I think the following is my solution...

Move MY_baseController.php from application/libraries and into application/controllers. Rename it to base.php and change the following line:

class baseController extends Template_Controller

into

class Base_Controller extends Template_Controller

Now within your Frontpage Controller, extend Base_Controller instead of baseController.

够运 2024-08-09 09:42:44

请务必遵循 Kohana 约定,以确保所有内容都能正确自动加载!模型助手和库也有类似的内容。

另外,如果您想保持主应用程序控制器文件夹干净,我建议为您的应用程序制作一个 Kohana 模块,并将所有模板和杂项扩展控制器放在那里,以使它们与主控制器分开。

只是不要忘记将模块添加到您的配置文件中!

Make sure you follow Kohana Conventions to make sure everything auto-loads properly! 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.

Just don't forget to add the module to your config file!

橘寄 2024-08-09 09:42:44

我知道这是一个老问题,但我想我应该说一句话。您只需从文件名中删除 MY_ 前缀,因为只有在扩展系统文件夹中以 _Core 为后缀的类时才真正需要它。例如,文件

class Controller extends Controller_Core

名为 MY_Controller.php。

在这种情况下,只需将文件命名为 baseController.php 并将其放入库文件夹中即可。

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. For example, the file for

class Controller extends Controller_Core

would be named MY_Controller.php.

In this case, just naming the file baseController.php and putting it in the libraries folder would work.

肩上的翅膀 2024-08-09 09:42:44

无意冒犯,但我不得不用头撞我的电脑才能让它与 Kohana 3.1 一起工作。我终于发现扩展模板控制器的语法应该是:

class Controller_Base extends Controller_Template

No offense, but I had to bang my head on my computer to get it working with Kohana 3.1. I finally figured out that the syntax to extend Template Controller should be:

class Controller_Base extends Controller_Template
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文