扩展 Kohana 中的模板控制器
我在尝试为我的项目追溯创建新的基本控制器时遇到了一些困惑。如果我没记错的话,我需要做的就是在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
经过一番摆弄,我认为以下是我的解决方案...
将
MY_baseController.php
从application/libraries
移动到application/控制器。将其重命名为
base.php
并将以下行更改为
Now 在 Frontpage 控制器中,扩展
Base_Controller
而不是baseController
。After some fiddling, I think the following is my solution...
Move
MY_baseController.php
fromapplication/libraries
and intoapplication/controllers
. Rename it tobase.php
and change the following line:into
Now within your Frontpage Controller, extend
Base_Controller
instead ofbaseController
.请务必遵循 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!
我知道这是一个老问题,但我想我应该说一句话。您只需从文件名中删除 MY_ 前缀,因为只有在扩展系统文件夹中以 _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
would be named MY_Controller.php.
In this case, just naming the file baseController.php and putting it in the libraries folder would work.
无意冒犯,但我不得不用头撞我的电脑才能让它与 Kohana 3.1 一起工作。我终于发现扩展模板控制器的语法应该是:
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: