CodeIgniter - 在哪里放置函数/类?

发布于 2024-12-31 22:27:11 字数 594 浏览 0 评论 0原文

我无法理解 CI 中的类应该保存在哪里。我正在构建一个描述/营销手机的应用程序。

我希望我的所有功能(即 getphone、getdetails 等)都驻留在一个名为 Mobile 的类中 - 我知道该文件应称为 Mobile.php 并驻留在controllers 文件夹中。

那么我可以在 Mobile.php 中拥有多个函数吗?例如

public function getphone() {
   xxx
   xx
   xx
}

public function getdetails() {
   xxx
   xx
   xx
}

,或者我需要将每个函数放在自己的类中吗?

我真的很感激查看一些有效的示例代码。我已经浏览了文档和谷歌几个小时,并尝试了 URL 中的各种变体来找到测试类,但运气不佳!我什至搞乱了路线和 .htaccess...

我想要实现的目标如下:

http:///model/HTC-Desire/ 重新路由到接受 HTC-Desire 作为的函数一个参数(因为我需要它来进行数据库查找)。默认控制器工作正常,但此后无法让任何东西工作。

有什么想法吗?

谢谢

Am having problems understanding where classes should be kept in CI. I am building an application that describes / markets mobile phones.

I would like for all of my functions (i.e. getphone, getdetails etc.) to reside in one class called Mobile - I understand that this file should be called Mobile.php and reside in the controllers folder.

Can I then have multiple functions inside Mobile.php? E.g.

public function getphone() {
   xxx
   xx
   xx
}

public function getdetails() {
   xxx
   xx
   xx
}

Or do I need to put each function in its own class?

I'd really appreciate looking at some sample code that works. I've been going through the documentation and google for a few hours, and tried all sorts of variations in the URL to find a test class, but without much luck! I've even messed around with the routes and .htaccess...

All I am trying to achieve is the following:

http:///model/HTC-Desire/ to be re-routed to a function that accepts HTC-Desire as a parameter (as I need it for a DB lookup). The default controller works fine, but can't get anything to work thereafter.

Any ideas?

Thanks

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

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

发布评论

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

评论(4

清风挽心 2025-01-07 22:27:11

实际上它的工作原理是这样的:

如您所知,控制器和模型进入它们的透视文件夹

如果您想创建不是对象方法的函数,则必须创建一个帮助程序文件。更多信息在这里:
http://codeigniter.com/user_guide/general/helpers.html

现在,如果您想创建自己的数据类型(不扩展模型和控制器的类),您将它们添加到库文件夹中。因此,如果您想创建一个类“Car”,您可以创建此文件:

class Car{

   function __construct(){}
}

并将其保存在库文件夹中作为 car.php

要创建 Car 类的实例,您必须执行以下操作:

$this->load->library('car');
$my_car = new Car();

有关库的更多信息,请参见此处:< br>
http://codeigniter.com/user_guide/general/creating_libraries.html

Actually it works like this:

Controllers and Models go to their perspective folders as you know it

If you want to create functions that are not methods of an object, you must create a helper file. More info here :
http://codeigniter.com/user_guide/general/helpers.html

Now if you want to create your own datatypes (classes that don't extend Models and Controllers), you add them to the library folder. So if let's say you want to create a class "Car" you create this file:

class Car{

   function __construct(){}
}

and save it in the libraries folder as car.php

To create an instance of the Car class you must do the following:

$this->load->library('car');
$my_car = new Car();

More information on libraries here:
http://codeigniter.com/user_guide/general/creating_libraries.html

野稚 2025-01-07 22:27:11

是的,您可以在控制器类中拥有任意数量的函数。它们可以通过 url /class/function 访问。

您可以捕获类函数中的参数,但不建议这样做。

class Mobile extends CI_Controller{

    public function getPhone($phoneModel=''){
        echo $phoneModel;
        //echo $this->input->post('phoneModel');
    }
}

http://site.com/mobile/getPhone/HTC-Rad 理论上会回显“HTC-Rad”。 但是,默认情况下,CI 中的 URL 中不欢迎特殊字符,因此在本示例中,您可能会遇到“不允许的 URI 字符”错误。您最好传递手机型号(或任何其他参数)通过 $_POST 发送到控制器。

Yes, you can have as many functions in a controller class as you'd like. They are accessible via the url /class/function.

You can catch parameters in the class functions, though it's not advisable.

class Mobile extends CI_Controller{

    public function getPhone($phoneModel=''){
        echo $phoneModel;
        //echo $this->input->post('phoneModel');
    }
}

http://site.com/mobile/getPhone/HTC-Rad theoretically would echo out "HTC-Rad". HOWEVER, special characters are not welcome in URL's in CI by default, so in this example you may be met with a 'Disallowed URI characters" error instead. You'd be better off passing the phone model (or any other parameters) via $_POST to the controller.

此生挚爱伱 2025-01-07 22:27:11

类可以同时作为控制器和模型存在,因为 CodeIgniter 实现了 MVC 模式。我建议阅读更多相关内容,以了解您的类/函数/等是如何实现的。可以最好地组织起来。

在我的脑海中,Pyro CMS 是一个使用 CodeIgniter 构建的应用程序,源代码是免费提供的。我确信还有其他人。

Classes can exist both as Controllers and Models, as CodeIgniter implements the MVC pattern. I recommend reading more about that to understand how your classes/functions/etc. can best be organized.

Off the top of my head, Pyro CMS is an application built with CodeIgniter and the source code is freely available. I'm sure there are others.

春花秋月 2025-01-07 22:27:11

我认为最好从一个角度来处理它,即:创建一个实用程序类,其中包含您的所有函数。
在哪里放置类文件的问题的答案是“libraries”文件夹。
文档中明确说明了这一点。 将您的类放入库文件夹中

当我们使用术语“库”时,我们通常指的是
位于库目录中并在中描述的类
本用户指南的类参考。

您可以阅读有关创建和使用库的更多信息创建库 — CodeIgniter 3.1.10 文档

将新创建的类放入库文件夹后,只需将库加载到控制器中即可使用,如下所示:

$this->load->library('yourphpclassname');

如果您希望在构造函数中接收多个参数,则必须修改它以接收参数这将是一个数组,您的加载/初始化会略有不同,如下所示:

$params = array('type' => 'large', 'color' => 'red');

$this->load->library('yourphpclassname', $params);

然后,要访问类中的任何函数,只需执行如下所示的操作:

$this->yourphpclassname->some_method();

我希望这能回答您的问题,如果您还有其他问题,请留下评论,我会很好地回应他们。

I think it's best you handle it from one perspective, that is; create a utility class with all your functions in it.
The answer to the question of where to put/place the class file is the "libraries" folder.
This is clearly stated in the documentation. Place your class in the libraries folder.

When we use the term “Libraries” we are normally referring to the
classes that are located in the libraries directory and described in
the Class Reference of this user guide.

You can read more on creating and using libraries Creating Libraries — CodeIgniter 3.1.10 documentation

After placing the newly created class in the libraries folder, to use just simply load the library within your controller as shown below:

$this->load->library('yourphpclassname');

If you wish to receive several arguments within you constructor you have to modify it to receive an argument which would be an array and you loading/initialization would then be slightly different as shown below:

$params = array('type' => 'large', 'color' => 'red');

$this->load->library('yourphpclassname', $params);

Then, to access any of the functions within the class simply do that as shown below:

$this->yourphpclassname->some_method();

I hope this answers your question if you have further question do leave a comment and I would do well to respond to them.

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