Codeigniter 构造函数 - 加载数据库 +设置变量

发布于 2024-12-17 08:52:55 字数 751 浏览 0 评论 0原文

我想知道如何将以下代码添加到我的 codeigniter 构造函数中:

$this->load->model('members_model');
            $member = $this->session->userdata('email_address');
            $viewdata['pagecontent'] = $this->members_model->get_profile($member);

该代码在我的整个控制器中使用,每次都重复它似乎很愚蠢。当我尝试将其添加到构造函数时,我无法引用设置的变量。

这是到目前为止的构造函数:

public function __construct(){
        parent::__construct();  
        Accesscontrol_helper::is_logged_in_super_user();    

        $this->load->model('members_model');
        $member = $this->session->userdata('email_address');
        $viewdata['pagecontent'] = $this->members_model->get_profile($member);
    }

为什么上面的代码不起作用?构造函数需要不同的代码吗?

I would like to know how I can add the following code to my codeigniter constructor:

$this->load->model('members_model');
            $member = $this->session->userdata('email_address');
            $viewdata['pagecontent'] = $this->members_model->get_profile($member);

The code is used throughout my controller and it seems silly to repeat it every time. When I try adding it to the constructor I am unable to reference the set variables.

This is the constructor so far:

public function __construct(){
        parent::__construct();  
        Accesscontrol_helper::is_logged_in_super_user();    

        $this->load->model('members_model');
        $member = $this->session->userdata('email_address');
        $viewdata['pagecontent'] = $this->members_model->get_profile($member);
    }

Why wouldn't the above code work? Do constructors require different code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放飞的风筝 2024-12-24 08:52:55

从这个意义上说,构造函数的工作方式与所有其他方法(以及类似函数)一样,因此您的变量受变量范围的约束。

执行:

class Mycontroller extends CI_Controller {

public $viewdata = array();

    function __construct()
    {
      parent::__construct();
      Accesscontrol_helper::is_logged_in_super_user();
      $this->load->model('members_model');
      // $this->load->library('session');
      // $this->load->database();
      // ^- if not already autoloaded
      $member = $this->session->userdata('email_address');
      $this->viewdata['pagecontent'] = $this->members_model->get_profile($member);
    }
}

然后,在其他控制器的方法中,只需调用类属性 $this->viewdata,例如。

function index()
{
  $this->load->view('myview',$this->viewdata);
}

并在 myview.php 中访问它:

echo $pagecontent;

Constructor works, in this sense, like all other methods (and like functions), so your vars are subject to the variable scope.

Do:

class Mycontroller extends CI_Controller {

public $viewdata = array();

    function __construct()
    {
      parent::__construct();
      Accesscontrol_helper::is_logged_in_super_user();
      $this->load->model('members_model');
      // $this->load->library('session');
      // $this->load->database();
      // ^- if not already autoloaded
      $member = $this->session->userdata('email_address');
      $this->viewdata['pagecontent'] = $this->members_model->get_profile($member);
    }
}

Then, in your other controller's methods, you just call the class property $this->viewdata, ex.

function index()
{
  $this->load->view('myview',$this->viewdata);
}

And access it, in myview.php :

echo $pagecontent;
裸钻 2024-12-24 08:52:55

您似乎遇到了一些范围问题。由于变量是在 __construct() 方法内部声明的,因此这是唯一能够引用它们的方法。您需要将它们设为类变量,以便在所有方法中访问它们。

尝试这样的方法:

class Your_class extends CI_Controller {

    protected $member;
    protected $viewdata;

    public function __construct()
    {
        parent::__construct();

        Accesscontrol_helper::is_logged_in_super_user();    

        $this->load->model('members_model');

        $this->member = $this->session->userdata('email_address');
        $this->viewdata['pagecontent'] = $this->members_model->get_profile($member);
    }
}

然后您可以在其他方法中引用 $member$viewdata ,如下所示: $this->member

您可以想要稍微不同地设置它,但希望您了解变量和范围。

It looks like you are having some scope issues. Since the variables are declared inside of the __construct() method, that is the only method that is able to reference them. You would need to make them class variables in order to have access to them in all of your methods.

Try something like this:

class Your_class extends CI_Controller {

    protected $member;
    protected $viewdata;

    public function __construct()
    {
        parent::__construct();

        Accesscontrol_helper::is_logged_in_super_user();    

        $this->load->model('members_model');

        $this->member = $this->session->userdata('email_address');
        $this->viewdata['pagecontent'] = $this->members_model->get_profile($member);
    }
}

Then you can reference $member and $viewdata in your other methods like this: $this->member

You may want to set this up a little differently, but hopefully you get the idea about variables and scope.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文