CI:在控制器中使用相同功能的最佳实践 [英] CI: Best practice for use same function in controller

查看:98
本文介绍了CI:在控制器中使用相同功能的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类名称​​ Deal ,并且其中有一个名为 addit

的函数。

  class Deal扩展了CI_Controller {
public function addit(){
//我的函数
}
}

我还有另一个控制器名称​​ Public ,我想使用属于Deal类的函数 addit

  class公共扩展交易{
public function addit(){
// my函数
}
}

我在Codeigniter中很新,只读了一些参考资料,在这个网站上查看另一个问题,已经有了一些答案,但是我不知道哪个是最佳实践,下面是我找到的一些解决方案:



1。创建 Deal 文件并保存在应用程序/库函数中,以便另一个控制器可以使用其功能。



2。在应用程序上创建 addit 函数ion / helper,以便我们可以在任何地方使用它的功能。



3。在application / core中创建 Deal 文件,以便其他控制器可以使用其功能。



我的问题是,解决我的问题的最佳实践是什么,还有其他解决方案吗?

解决方案

这取决于我们在这里讨论的功能类型。
如果它是一个非常简单的功能,它不执行任何数据库访问或与CI组件交互,则可以创建一个帮助器:



codeigniter文档:


帮助程序不是以面向对象的格式编写的。它们是简单,是
程序功能。每个助手功能执行一个特定的任务,
而不依赖于其他功能。


http://www.codeigniter.com/user_guide/general/helpers.html






但是,如果是相对复杂的代码,则应考虑编写库:



在应用程序/库中创建Tools.php(或任何您想要的文件)

 <?php if(!define('BASEPATH' ))exit('不允许直接脚本访问'); 

类工具
{
受保护的$ ci;

公共功能__construct()
{
$ this-> ci =& get_instance();
//允许您访问CI组件
}

公共功能myfunction($ data)
{
//如果要使用codeigniter的功能:
//使用$ this-> ci-> function()
}

}

然后在您的控制器中:

  $ this-> load- > library( tools); 
$ this->工具-> myfunction($ data);

如果要经常使用您的库,则可以在config /中全部加载一次autoload.php:

  $ autoload ['libraries'] = array('database','tools','... '); 

有关库的CI文档: http://www.codeigniter.com/user_guide/general/creating_libraries.html






选项3不适合您的问题,因为您没有扩展CI本身。



来自评论:顾名思义,核心是用于改善CI的核心。例如,如果您不喜欢CI处理会话的方式,则可以编写一个新的核心类,但是,如果它与您的用户管理系统有关,那么它与您的应用有关,而与框架无关,应该在lib中完成。 / p>

I have class name Deal and have function inside it which name is addit

class Deal extends CI_Controller {
   public function addit() {
      //my function
   }
}

And I have another controller name Public and I want to use function addit which belong to class Deal by extend it.

class Public extends Deal {
       public function addit() {
          //my function
       }
}

I quite new in Codeigniter and just read few references, looking at another question on this site, and already had some answers, but I dont know which one is the best practice for it, here some solution I found:

1.Create Deal file and save at application/libraries function so another controller can use its function.

2.Create addit function on application/helper so we can use its function anywhere.

3.Create Deal file in application/core so other controller can use its function.

My question is, what is the best practice for solving my problem, and are there any other solution?

解决方案

It depends on what type of function we are talking about here. If it is about really simple function which don't do any db access or have interaction with CI components, you can create an helper :

Quote from the codeigniter documentation :

Helpers are not written in an Object Oriented format. They are simple, procedural functions. Each helper function performs one specific task, with no dependence on other functions.

http://www.codeigniter.com/user_guide/general/helpers.html


However, if it's a relatively complex code, then you should consider writing a library :

In application/libraries create Tools.php (or whatever you want)

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Tools
{
  protected $ci;

    public function __construct()
    {
        $this->ci =& get_instance(); 
        //Allows you to access CI components
    }

    public function myfunction($data)
    {
       //If you want to use codeigniter's functionalities :
       //use $this->ci->function()
    }

}

And then in your controllers :

$this->load->library("tools");
$this->tools->myfunction($data);

If your library is meant to be used very often, you can load it once for all in config/autoload.php :

$autoload['libraries'] = array('database', 'tools', '...');

CI doc about libraries : http://www.codeigniter.com/user_guide/general/creating_libraries.html


Option 3 doesn't fit your problematic as you are not extending CI itself.

From comments : As the name suggests, "core" is for improving CI's core. If you don't like the way CI handles sessions for example, then you can write a new core class, however, if it's about your user management system then it concerns your app and not the framework and should be done in a lib.

这篇关于CI:在控制器中使用相同功能的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆