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 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100