xnettunnel 发表于 2012-3-3 15:44:35

Help: 想继承CI_Controller,弄个自己的Controller,结果。。。

一般做一个项目都应该有自己的父类,好做一致性的设置或控制,由于刚接触CI,也学者cicms的做法一样,想弄个My_Controller来,然后让所有的Controller继承My_Controller。

建立My_Controller.php,里面就是一个空壳:
abstract class My_Controller extends CI_Controller{
   function __construct(){
       parent::__construct();
   }
}
跟着cicms一样,把这个php放到application的core里面,然后在controllers文件夹下建立一个blog.php:
class Blog extends My_Controller{
   ...
}

结果,php报错说My_Controller没定义,吧My_Controller.php移到application/controllers下,跟blog.php同一个目录,也一样报错。

是什么问题呢?新手,问题是很傻...{:soso_e113:}

lynn.wang 发表于 2012-3-3 16:12:57


class MY_Input extends CI_Input {
    function __construct()
    {
      parent::__construct();
    }
}
官方说明的创建核心类的方法
abstract 拿掉
按照你描述的应该没有什么问题
controller下的文件中的类名首字母大写
然后你再试试
如果还报错请把错误信息贴出来
便于检查!!

xnettunnel 发表于 2012-3-3 18:43:10

本来没有abstract修饰的,也试过,不行啊。看到cicms用了这个我也用了,还是不行。

PHP Fatal error:Class 'M
y_Controller' not found in I:\\php\\APMServ5.2.6\\www\\htdocs\\ci1\\application\
\controllers\\blog.php on line 2

My_Controller.php
<?php
abstract class My_Controller extends CI_Controller{
        function __construct()
                {
                        parent::__construct();
                }
}
?>




blog.lphp

<?php
class Blog extends My_Controller{
        function __construct(){
                parent::__construct();
                //test $this->load->database();
                $this->load->model("posts_model");
                //$this->load->scaffolding('posts');
        }
        function index(){
                $data["title"]="My blog title";
                $data["heading"]="My blog heading";
                //$data["todo"]=array("clear house","eat lunch","call mom");
                $data["posts"]=$this->posts_model->get_posts();
                $this->load->view("blog_view",$data);
        }
        public function view($id){
                if (!$id){
                        show_error("Invalid URL",404);
                        return;
                }
                $data['post']=$this->posts_model->get_posts($id);
                if (empty($data['post'])){
                        show_404();
                }
                $data['title']=$data['post']['title'];
                $data['body']=$data['post']['body'];
                $this->load->view("posts/post_view",$data);
        }
}
?>

xnettunnel 发表于 2012-3-3 21:47:34

:lol:lol:lol刚在同事的监视下,重新把My_controller.php移到application/core下,又行了,怪事!!

lynn.wang 发表于 2012-3-4 15:04:34

崩溃!!!

vergil 发表于 2012-3-5 13:50:35

我一般放在application/controllers下
然后在子类开始前先require进来

水月刀猪 发表于 2012-3-6 17:27:57

。。。。。。。。。这要放到core下才行

洗礼 发表于 2012-3-6 17:29:31

本帖最后由 洗礼 于 2012-3-6 17:36 编辑

我也有同样的问题。。。
报错。。
An Error Was Encountered

Unable to load the requested class: my_controller
my_controller放在core下的。。

水月刀猪 发表于 2012-3-6 17:38:38

建立My_Controller.php,里面就是一个空壳:
class My_Controller extends CI_Controller{
   function __construct(){
       parent::__construct();
   }
}
规范写
页: [1]
查看完整版本: Help: 想继承CI_Controller,弄个自己的Controller,结果。。。