简体   繁体   English

在CodeIgniter中合并类/控制器/库的正确方法是什么?

[英]What is the correct way to incorporate classes/controllers/libraries in CodeIgniter?

I'm a Codeigniter rookie, though I have about 2 years of experience with PHP. 我是Codeigniter的新手,虽然我有大约2年的PHP经验。 I've been through the Codeigniter documentation, and I've created some simple examples, but now I'm moving on to an actual project and I want to make sure I get off on the right foot. 我已经阅读了Codeigniter文档,并且我已经创建了一些简单的示例,但现在我正在进行实际项目,我想确保我正确地走下去。

Here is the issue: I'm building a one-page game application, and on that page there will be lots of dynamic/DB data. 问题在于:我正在构建一个单页游戏应用程序,在该页面上会有很多动态/数据库数据。 I've created a controller and view for the page, so far it's all very simple: 我已经为页面创建了一个控制器和视图,到目前为止它非常简单:

//controller (application/controllers/main.php)
class Main extends CI_Controller {

    public function index()
    {
            $this->load->model('Model');        //load DB abstraction model
            $this->load->helper('url');     //load URL helper (base_url() function etc)

            $data = array("somestuff","otherstuff");//some general data for the page

        $this->load->view('header');
        $this->load->view('mainview', $data);
        $this->load->view('footer');
    }   
}

//view (application/views/mainview.php)
<div id="container">
    <div id="tab1">
        some data related to logged in user
    </div>
    <div id="tab2">
        some data related to game
    </div>
</div>

Now, you see, we can split the data that should be displayed on the page into two groups - user data and application data. 现在,您可以看到,我们可以将应该在页面上显示的数据分成两组 - 用户数据和应用程序数据。 The natural OOP approach would be to: 自然的OOP方法是:

1) create one User class and use a function within that to fetch and display user data (loading the Model as well since we're using MVC) 1)创建一个User类并使用其中的函数来获取和显示用户数据(因为我们正在使用MVC,所以加载模型)

2) create one or more game-related classes and use those to calculate and display current game data 2)创建一个或多个与游戏相关的类,并使用它们来计算和显示当前游戏数据

For simplicity, let's assume I just need one Game class. 为简单起见,我们假设我只需要一个Game类。

What I'm having trouble with is wrapping my mind about the correct CodeIgniter approach to this. 我遇到的问题是我想到了正确的CodeIgniter方法。 The user guide mentions user-created libraries, user-created controllers and user-created classes. 用户指南提到用户创建的库,用户创建的控制器和用户创建的类。 The libraries are a bit of a confusing concept to me since I don't understand how they make a difference here. 这些库对我来说是一个令人困惑的概念,因为我不明白它们如何在这里有所作为。

Since I already have a controller for the page (I think that's the natural approach too but please correct me if I'm wrong), how do I integrate the User/Game classes? 因为我已经有一个页面控制器(我认为这也是一种自然的方法,但如果我错了请纠正我),我如何整合用户/游戏类? I have a few specific questions that should help me get this straight in my head. 我有一些具体的问题可以帮助我直截了当地解决这个问题。

1) Should User/Game be new controllers that extend CI_Controller? 1)用户/游戏应该是扩展CI_Controller的新控制器吗?

2) Should the classes be placed in the "libraries" directory or directly in the controller? 2)类应该放在“库”目录中还是直接放在控制器中?

3) What relationship should they have with the Main controller (assuming I was correct in creating it in the first place)? 3)他们应该与主控制器有什么关系(假设我在创建它时是正确的)?

I give you some clue. 我给你一些线索。

From my experience and what i seen: 根据我的经验和我所看到的:

Controller is used when it serve formated data like text, json, html etc. 当控制器提供文本,json,html等格式化数据时使用。
Or used when it receive inputs and redirect to other page eventualy. 或者在接收输入并重定向到其他页面时使用。
Controller can have some privates methods used by other methods to format or check some data. 控制器可以使用其他方法使用的一些私有方法来格式化或检查某些数据。

Libraries can be bridge between Controllers and Models. 库可以在控制器和模型之间架起桥梁。
They works with models or other libraries or just native PHP. 它们适用于模型或其他库或仅使用本机PHP。
Commonly libraries works with data received by controllers and pass them to models after some check or work with them. 通常,库使用控制器接收的数据,并在检查或使用它们之后将它们传递给模型。

Libraries can be also classes,representing an entity, you have to instanciate it like $Obj = new SomeObject() (you have to include the class file). 库也可以是类,代表一个实体,你必须像$Obj = new SomeObject() (你必须包含类文件)。
In that case the Singleton pattern is not used. 在这种情况下,不使用Singleton模式。

  • So if your Controller receive the input credit_brought 因此,如果您的Controller收到输入credit_brought
  • it check if credit_brought is a number (eventualy) 检查credit_brought是否为数字(最终)
  • it call the proper class method $User->add_credit() 它调用正确的类方法$User->add_credit()
    • That method can rise an exception if credit_brought is not a number 如果credit_brought不是数字,那么该方法可能会引发异常
    • it can do some check like "is this user earn enough credit for that hour" 它可以做一些检查,如“这个用户是否在那个小时赚取足够的信用”
    • That method can call the model $this->user_model->add_credit() 该方法可以调用模型$this->user_model->add_credit()
      • To make the SQL Query, for example: $this->db->set('credit', 'credit +'.$credit, FALSE)->where('id', $userid)->update('users') 要进行SQL查询,例如: $this->db->set('credit', 'credit +'.$credit, FALSE)->where('id', $userid)->update('users')
  • When all done, $User->add_credit() return some response (bool in that case) to the controller 完成所有操作后, $User->add_credit() )会向控制器返回一些响应(在这种情况下为bool)
  • The controller return the appropriate data to the client (view or ajax response or..) 控制器将适当的数据返回给客户端(view或ajax response或..)

I'm not sure if i was clear with my poor english.. 我不确定我的英语能力是否清晰

In CI doc, read the Introduction and General Topics pages that you can find in the table of contents 在CI doc中,阅读可在目录中找到的“ 简介”和“ 常规主题”页面

Please correct me if you see any spelling mistakes. 如果你发现任何拼写错误,请纠正我。

I think it's better to separate the Game and User controller as they represent different resources. 我认为最好将游戏和用户控制器分开,因为它们代表不同的资源。 But to answer your questions by bullets: 但要用子弹回答你的问题:

  1. Yes UserController and GameController should extend CI_Controller to be able to respond incoming requests that comes from the front controller. 是UserController和GameController应该扩展CI_Controller,以便能够响应来自前端控制器的传入请求。
  2. They are placed in controllers folder by default. 它们默认位于controllers文件夹中。
  3. Well, the MainController's index() method creates the JS objects whose will interact with UserController and GameController methods using AJAX requests. 好吧,MainController的index()方法创建了JS对象,它们将使用AJAX请求与UserController和GameController方法进行交互。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM