douqiang6448 2013-12-26 15:35
浏览 23
已采纳

扩展核心CI_Controller

According to this Extending core CI Classes , I can create a class in my application/core/MY_Protectedcontroller.php as:

   //e.g. I want to extend core CI_Controller class.So
   class MY_Protectedcontroller extends CI_Controller{
      function __construct(){
        parent::__construct();
        $this->load->library('permission');
        $this->load->library('authentication');
      }
    }

Well as you might discern my objective here..actually I want the auth and perm libraries to be available to all of my controllers thus I can authenticate very easily and programmatically. But when I tried to do:

    class User extends MY_Protectedcontroller{
      function __construct(){
        parent::__construct();
        .....// relevant codes
        .....//relevant codes
      }
    }

It says the "Not Found" thing. Fatal error: Class 'MY_Protectedcontroller' not found in C:\xampp\htdocs\damcombd\application\controllers\admin\user.php on line 3

  • 写回答

3条回答 默认 最新

  • du1462 2013-12-26 16:45
    关注

    You have to name your class file MY_Controller.php, NOT MY_Protectedcontroller.php.

    As you can see in the user guide, in the System Class list, MY_Protectedcontroller is not there. You have to extend an existing class, and you have to name it appropriately for CodeIgniter to find it. In this case, we're extending Controller, so you have to name the file MY_Controller.php so that CodeIgniter recognizes and finds it (you can change MY_ in the config as you've found). If we were trying to extend the Input class, we would name the file MY_Input.php and so on.


    Download Codeigniter 2.1.4 and put it somehwere.

    Create MY_Controller.php and put it in application/core/MY_Controller.php:

    <?php defined('BASEPATH') OR exit('No direct script access allowed');
    
    class MY_Protectedcontroller extends CI_Controller
    {
        function __construct()
        {
            parent::__construct();
        }
    }
    

    Create a controller test.php and put it in controllers/test.php:

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class Test extends MY_Protectedcontroller
    {
        function index()
        {
            echo "test";
        }
    }
    

    and visit http://localhost/ci-2.1.4/index.php/test

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 深度学习残差模块模型
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)