codeigniter:扩展公共控制器 [英] codeigniter: extending common controller
问题描述
我已阅读所有关于此问题的帖子,但没有什么工作。我使用Codeigniter 2.02在一个LAMP与Apache2.2和PHP5.3.2
我试图创建一个公共控制器从其中我的公共控制器将继承所以我
我有这个:
file:parent_controller.php
if(!defined('BASEPATH'))exit('不允许直接脚本访问);
class Parent_controller extends CI_Controller {
public function Parent_controller()
{
parent :: __ construct();
}
public function index(){
echoHi!;
}
}
file:welcome.php
if(!defined('BASEPATH'))exit('No direct script access'
class Welcome extends Parent_controller {
public function __construct()
{
parent :: __ construct();
}
}
我试过下面的解决方案,我发现,但都没有工作:
-
public function __contstruct strong>而不是 public function Parent_controller()
-
parent :: Parent_controller
-
将parent_controller.php文件放入核心 >将parent_controller.php文件放入控制器
-
将其添加到config / config.php:
function __autoload($ class){
if(file_exists(APPPATH。(controllers | core)/。$ class.EXT)
require_once(APPPATH。'(controllers | core)/'.$ class.EXT);
}
}
谢谢所有人
看看这篇来自Phil Sturgeon的帖子:
http:// philsturgeon。 co.uk/blog/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY
关键是使用本地自动加载,如他的帖子:
/ *
| -------------------------------------------------- -----------------
|本机自动加载
| -------------------------------------------------- -----------------
|
|与cnfig / autoload.php无关,这允许PHP自动加载工作
|用于基本控制器和一些第三方库。
|
* /
function __autoload($ class)
{
if(strpos($ class,'CI_')!== 0)
{
@include_once(APPPATH。'core /'。$ class。EXT);
}
}
注意 >
注意,您需要将所有的base控制器放在CI2 + 文件夹的 core
p>
I've readed all the post I found regarding this issue but nothing works. I'm using Codeigniter 2.02 in a LAMP with Apache2.2 and PHP5.3.2
I'm trying to create a common controller from which my common controllers will inherit so I can do common tasks there.
I have this:
file: parent_controller.php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Parent_controller extends CI_Controller {
public function Parent_controller()
{
parent::__construct();
}
public function index() {
echo "Hi!";
}
}
file: welcome.php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends Parent_controller {
public function __construct()
{
parent::__construct();
}
}
I've tried the next solutions I've found, but none of them are working:
public function __contstruct() instead of public function Parent_controller()
parent::Parent_controller();
put the parent_controller.php file into core
put the parent_controller.php file into controllers
adding this to config/config.php:
function __autoload($class){ if (file_exists(APPPATH."(controllers|core)/".$class.EXT)){ require_once(APPPATH.'(controllers|core)/'.$class.EXT); } }
Thank you all guys
Take a look at this post from Phil Sturgeon:
http://philsturgeon.co.uk/blog/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY
The key is using the native autoload as explained in his post:
/*
| -------------------------------------------------------------------
| Native Auto-load
| -------------------------------------------------------------------
|
| Nothing to do with cnfig/autoload.php, this allows PHP autoload to work
| for base controllers and some third-party libraries.
|
*/
function __autoload($class)
{
if(strpos($class, 'CI_') !== 0)
{
@include_once( APPPATH . 'core/'. $class . EXT );
}
}
NOTE
As a note, you'll want to put all of your "base" controllers in the core
folder for CI2+
这篇关于codeigniter:扩展公共控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!