0

开门见山 :

我正在使用 Kohana,并且正在查看另一个用普通 PHP 编写的脚本。在脚本中,我有一个类 ShoppingCart。如果我要将脚本转换为 Kohana,我在哪里放置类、它的方法和它的属性?

它在我现有的默认控制器中吗?或者我应该把它放在一个单独的控制器中?或者听起来很菜鸟,我会把它放在模型中吗?

4

4 回答 4

0

As per kohana convention, you should place custom classes in application/libraries folder. However for this, you need to know how to get the class to work after putting it there. If you can't figure that out, you can do anything like putting it in your controller or making another controller of it, etc.

于 2009-12-18T06:02:38.700 回答
0

That depends on the specifics of the class I suppose. To be honest I don't know anything about Kohana, but there's probably a place for "vendor files" somewhere. Maybe it's best to place it there and write wrapper functions for it in your controller. If the class already integrates well with Kohana you may instead choose to use it as a controller or model directly. Or you might want to take the time to rewrite it to make it work as a controller...

Only you can evaluate the best place for it, there's no hard and fast rule here.

于 2009-08-05T07:47:09.910 回答
0

Kohana has a folder for 3rd party libraries. The main one is under system/vendor, you can put it in you application/ as well.

Many PHP class loaders require details like your filename should be the same as the class name (at least that's what I read in the Kohana documentation) if you want the classes to be automatically loaded.

于 2009-08-05T07:48:30.563 回答
0

如果您需要在您的应用程序中使用第 3 方代码,建议您在您的应用程序/模块文件夹中创建一个名为“vendor”的文件夹,并将所有代码放在那里。

然后,您可以通过调用包含文件:

include kohana::find_file('vendor', 'filename');

如果需要,您还可以为外部库创建一个包装器,一个很好的例子是使用 3rd 方 Swift 电子邮件库的电子邮件助手。

如果您要将自己的课程移植到 kohana,那么您需要弄清楚该课程将做什么并相应地对其进行分类。

如果该类将从某种数据库中获取项目,那么您应该将其设为模型。库通常是您希望跨控制器/模型重用的代码集,例如身份验证、日历生成等。控制器用于将数据从模型传递到您的视图/库。

有关更多信息,请参阅文档

于 2009-08-12T21:32:50.160 回答