如何扩展代码igniter控制器类? [英] How do I extend the code igniter controller class?

查看:116
本文介绍了如何扩展代码igniter控制器类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的CI系统\libraries目录中,我有一个名为DD_Controller.php的新类。此文件如下所示:

 <?php if(!defined('BASEPATH'))exit允许访问); 

类DD_Controller extends Controller
{
protected $ ddauthentication;


function __construct()
{
parent :: Controller();
$ this-> ddauthentication =Authenticated;
}
}
?>

我的应用程序控制器定义如下:

  class Inquiry DD_Controller 
{...}

当我扩展Controller时,Inquiry类工作正常,但我得到一个


致命错误:类'DD_Controller'不是

中找到C:\development\localhost\applications\inquiry\controllers\inquiry.php
在第4行上


当我扩展DD_Controller。在配置文件中我有如下定义的前缀:

  $ config ['subclass_prefix'] ='DD_'; 

有什么想法我缺少什么?



TIA

解决方案

DD_Controller.php应位于/ system / application / libraries /



如果您对多个应用程序使用相同的CI,并且希望它们都能将其控制器扩展到您的自定义控制器,那么您可以在同一文件中扩展基本的Controller类。 p>

在Controller类下面的系统/ libraries / Controller.php中:

  class Mega_Controller extends Controller {
function Mega_Controller()
{
parent :: Controller();
//任何你想在每个控制器中做的事,你们都要在这里执行。
}
}

app controllers:

  class Home extends Mega_Controller {
....

由于您创建的扩展控制器类将可用。我认为这是更好的覆盖基本控制器,但这将工作,以及。


In my CI system\libraries directory I have a new class named DD_Controller.php. This file looks like this:

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

class DD_Controller extends Controller 
{   
    protected $ddauthentication;


    function __construct()
    {           
        parent::Controller();
        $this->ddauthentication = "Authenticated";
    }
}
?>

My application controller is defined like this:

class Inquiry extends DD_Controller 
{...}

The Inquiry class works fine when I extend Controller, but I get a

Fatal error: Class 'DD_Controller' not found in C:\development\localhost\applications\inquiry\controllers\inquiry.php on line 4

When I extend DD_Controller. In the config file I have the prefix defined as such:

$config['subclass_prefix'] = 'DD_';

Any idea of what I'm missing?

TIA

解决方案

DD_Controller.php should be in /system/application/libraries/

If you're using the same CI for multiple apps, and you want them all to be able to extends their controllers to your custom one then you can extend the base Controller class in the same file.

In system/libraries/Controller.php below the Controller class:

class Mega_Controller extends Controller {
    function Mega_Controller()
    {
        parent::Controller();
        // anything you want to do in every controller, ye shall perform here.
    }
}

Then you'll be able to do this in your app controllers:

class Home extends Mega_Controller {
    ....

Since the extended controller class you created will be available. I think this is better then overwriting the base controller, but that would work as well.

这篇关于如何扩展代码igniter控制器类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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