在MVC上下文中,我在哪里放一个类?

时间:2022-09-01 14:27:21

straight to the point :

开门见山 :

I am using Kohana, and I am looking at another script written in plain PHP. In the script, I have a class ShoppingCart. If I am to convert the script to Kohana, where am I to put the class, its methods and its properties?

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

Is it in my existing default controller? Or should I put it in a separate controller? Or as noobie as it may sound, will I put it in the model?

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

4 个解决方案

#1


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...

这取决于我认为的课程的具体情况。说实话,我对Kohana一无所知,但在某处可能存在“供应商文件”的地方。也许最好把它放在那里并在你的控制器中为它编写包装函数。如果该类已经与Kohana很好地集成,您可以选择直接将其用作控制器或模型。或者您可能想花时间重写它以使其作为控制器工作......

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

只有你可以评估它的最佳位置,这里没有硬性规定。

#2


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.

Kohana有一个第三方库的文件夹。主要是在系统/供应商之下,您也可以将它放在您的应用程序/中。

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.

许多PHP类加载器需要详细信息,例如您的文件名应该与类名相同(至少我在Kohana文档中读到的内容),如果您希望自动加载类。

#3


0  

If you need to use 3rd party code in your app it's recommended that you create a folder in your app / module folder called 'vendor' and place all of that code there.

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

You can then include the files by calling:

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

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

If needs be you can also create a wrapper for the external library, a good example of this is the email helper which uses the 3rd party Swift email library.

如果需要,您还可以为外部库创建一个包装器,一个很好的例子就是使用第三方Swift电子邮件库的电子邮件帮助程序。

If you're porting your own class to kohana then you need to work out what the class will be doing and categorise it accordingly.

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

If the class will be fetching items from some kind of database then you should make it a model. Libraries are usually sets of code that you want reuse across controllers / models, such as authentication, calendar generation etc. Controllers are used for passing data from models to your views / libraries.

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

See the docs for more info

有关详细信息,请参阅文档

#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.

根据kohana惯例,您应该将自定义类放在application / libraries文件夹中。但是,为此,您需要知道如何在将类放入其中后使其工作。如果你无法弄明白,你可以做任何事情,比如把它放在你的控制器或制作它的另一个控制器等。

#1


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...

这取决于我认为的课程的具体情况。说实话,我对Kohana一无所知,但在某处可能存在“供应商文件”的地方。也许最好把它放在那里并在你的控制器中为它编写包装函数。如果该类已经与Kohana很好地集成,您可以选择直接将其用作控制器或模型。或者您可能想花时间重写它以使其作为控制器工作......

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

只有你可以评估它的最佳位置,这里没有硬性规定。

#2


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.

Kohana有一个第三方库的文件夹。主要是在系统/供应商之下,您也可以将它放在您的应用程序/中。

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.

许多PHP类加载器需要详细信息,例如您的文件名应该与类名相同(至少我在Kohana文档中读到的内容),如果您希望自动加载类。

#3


0  

If you need to use 3rd party code in your app it's recommended that you create a folder in your app / module folder called 'vendor' and place all of that code there.

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

You can then include the files by calling:

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

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

If needs be you can also create a wrapper for the external library, a good example of this is the email helper which uses the 3rd party Swift email library.

如果需要,您还可以为外部库创建一个包装器,一个很好的例子就是使用第三方Swift电子邮件库的电子邮件帮助程序。

If you're porting your own class to kohana then you need to work out what the class will be doing and categorise it accordingly.

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

If the class will be fetching items from some kind of database then you should make it a model. Libraries are usually sets of code that you want reuse across controllers / models, such as authentication, calendar generation etc. Controllers are used for passing data from models to your views / libraries.

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

See the docs for more info

有关详细信息,请参阅文档

#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.

根据kohana惯例,您应该将自定义类放在application / libraries文件夹中。但是,为此,您需要知道如何在将类放入其中后使其工作。如果你无法弄明白,你可以做任何事情,比如把它放在你的控制器或制作它的另一个控制器等。