codeigniter:扩展公共控制器 [英] codeigniter: extending common controller

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

问题描述

我已阅读所有关于此问题的帖子,但没有什么工作。我使用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('不允许直接脚本访问); 

class Welcome extends Parent_controller {

public function __construct()
{
parent :: __ construct();
}
}

我试过下面的解决方案,但它们都不工作:

public function __contstruct()而不是 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 read 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.

    解决方案

    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屋!

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