CI:在控制器中使用相同功能的最佳实践
CI: Best practice for use same function in controller
我有 class 名称 Deal 并且里面有函数,名称是 addit
class Deal extends CI_Controller {
public function addit() {
//my function
}
}
我还有另一个控制器名称 Public 我想使用属于 [=49= 的函数 addit ] 延长处理。
class Public extends Deal {
public function addit() {
//my function
}
}
我是 Codeigniter 的新手,只阅读了一些参考资料,查看了本网站上的另一个问题,并且已经有了一些答案,但我不知道哪个是 最佳实践为此,我找到了一些解决方案:
1.Create 交易 文件并保存在 application/libraries 函数中以便另一个控制器可以使用它的函数。
2.Create 在 application/helper 上添加 函数,以便我们可以在任何地方使用它的函数。
3.Create Deal 文件 application/core 以便其他控制器可以使用其功能。
我的问题是,解决我的问题的最佳做法是什么,还有其他解决方案吗?
这取决于我们在这里谈论的是什么类型的函数。
如果它是关于不进行任何数据库访问或与 CI 组件交互的非常简单的函数,您可以创建一个帮助程序:
引自 codeigniter 文档:
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
但是,如果它是一个相对复杂的代码,那么你应该考虑编写一个库:
在 application/libraries 中创建 Tools.php(或任何你想要的)
<?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()
}
}
然后在你的控制器中:
$this->load->library("tools");
$this->tools->myfunction($data);
如果您的库需要经常使用,您可以在 config/autoload.php 中一次性加载它:
$autoload['libraries'] = array('database', 'tools', '...');
CI 关于库的文档:http://www.codeigniter.com/user_guide/general/creating_libraries.html
选项 3 不适合您的问题,因为您没有扩展 CI 本身。
来自评论 : 顾名思义,"core" 是为了改进 CI 的核心。例如,如果您不喜欢 CI 处理会话的方式,那么您可以编写一个新的核心 class,但是,如果它与您的用户管理系统有关,那么它与您的应用程序有关,而不是框架和应该在库中完成。
我有 class 名称 Deal 并且里面有函数,名称是 addit
class Deal extends CI_Controller {
public function addit() {
//my function
}
}
我还有另一个控制器名称 Public 我想使用属于 [=49= 的函数 addit ] 延长处理。
class Public extends Deal {
public function addit() {
//my function
}
}
我是 Codeigniter 的新手,只阅读了一些参考资料,查看了本网站上的另一个问题,并且已经有了一些答案,但我不知道哪个是 最佳实践为此,我找到了一些解决方案:
1.Create 交易 文件并保存在 application/libraries 函数中以便另一个控制器可以使用它的函数。
2.Create 在 application/helper 上添加 函数,以便我们可以在任何地方使用它的函数。
3.Create Deal 文件 application/core 以便其他控制器可以使用其功能。
我的问题是,解决我的问题的最佳做法是什么,还有其他解决方案吗?
这取决于我们在这里谈论的是什么类型的函数。 如果它是关于不进行任何数据库访问或与 CI 组件交互的非常简单的函数,您可以创建一个帮助程序:
引自 codeigniter 文档:
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
但是,如果它是一个相对复杂的代码,那么你应该考虑编写一个库:
在 application/libraries 中创建 Tools.php(或任何你想要的)
<?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()
}
}
然后在你的控制器中:
$this->load->library("tools");
$this->tools->myfunction($data);
如果您的库需要经常使用,您可以在 config/autoload.php 中一次性加载它:
$autoload['libraries'] = array('database', 'tools', '...');
CI 关于库的文档:http://www.codeigniter.com/user_guide/general/creating_libraries.html
选项 3 不适合您的问题,因为您没有扩展 CI 本身。
来自评论 : 顾名思义,"core" 是为了改进 CI 的核心。例如,如果您不喜欢 CI 处理会话的方式,那么您可以编写一个新的核心 class,但是,如果它与您的用户管理系统有关,那么它与您的应用程序有关,而不是框架和应该在库中完成。