我的base_url没有工作,虽然我自动加载它..为什么?

时间:2013-12-30 20:05:59

标签: php codeigniter

$autoload['helper'] = array('url', 'form');

class user extends CI_Controller

{

    function user()
    {
        $this->view_data['base_url']= base_url();
    }

    function index()
    {
        $this->regester();
    }
    function regester()
    {
        $this->load->view('user_view',$this->view_data);
    }
}

1 个答案:

答案 0 :(得分:1)

尝试使用适用于php5的构造函数:

class User extends CI_Controller
{
    private $view_data;

    public function __construct()
    {
        parent::__construct(); // necessary in CI if you declare a constructor
        $this->view_data['base_url']= base_url();
    }
}
  

为了向后兼容,如果PHP 5找不到__construct()   给定类的函数,并且该类没有从a继承   父类,它将搜索旧式构造函数,   按类的名称

由于您的班级是具有__construct()方法的CI_Controller的子级,我相信您的类中的方法User()在实例化时不会被调用,base_url()也是如此。 p>