Welcome Guest, Not a member yet? Register   Sign In
Using the session class in a custom class
#1

[eluser]Emass[/eluser]
Hello

I just recently started using codeiginter, and what I'd like to do is to have a class which extends my Page class which extends the Controller.
The code below works just fine, but I cannot access the session class in the Main class. The parent:Tongueage; is giving an error now, but when I take it out the $this of '$this->session->set_userdata($session);' does not refer to the session class. Is there any way how I can get my main class to get to extend both the Page class and the Controller, so I can use stuff like the session class and the uri segment, in the Main class?
I'd appreciate the help.

Code:
<?php
class Page extends Controller {

    function __construct() {
        parent::Controller();
        global $main;

        $main->setPageid($this->uri->segment(3, $main->defaultPageid));
    }

    function index() {
        global $main;
        $this->load->model('default_queries');

        $data['menu'] = $this->default_queries->getMenu();
        $data['content'] = $this->default_queries->getContent($main->getPageid());
        $data['title'] = $main->getTitle();
        $this->load->view('index', $data);
    }

    function cms() {
        global $main;
        //var_dump($main->getLogin());

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

        $data['menu'] = $this->default_queries->getMenu();
        $data['content'] = $this->default_queries->getContent($main->getPageid());
        $data['title'] = $main->getTitle();
        $this->load->view('index', $data);
    }
}
class Main extends Page {

    public $defaultPageid = 1;
    private $pageid;
    private $title;
    private $login = false;

    function __construct() {
        parent::Page;

        $session = array(
                   'id'  => '1'
        );

        $this->session->set_userdata($session);

        $this->setTitle();
    }


    function getLogin(){
        return $this->login;
    }
    function setTitle() {
        $this->title = 'Title...';
    }
    function getTitle() {
        return $this->title;
    }
    function setPageid($id) {
        $this->pageid = $id;
    }
    function getPageid() {
        return $this->pageid;
    }
}
$main = new Main;
#2

[eluser]jedd[/eluser]
Hi Emass and welcome to the CI forums.

[quote author="Emass" date="1259426937"]
The parent:Tongueage; is giving an error now ...
[/quote]

Are we allowed to know what the error is?

Looking through your code, I suspect there's an easy answer - extend the Controller using the MY_Controller approach, rather than trying to extend it to Page and then extend that to Main. I'm also not sure what kind of problems you invite by having two classes in one controllers/ file - but I suspect it would likely confuse CI.

Read through the user guide and the wiki on extending core libraries - as I said, I think this will be a much cleaner way of doing what you're trying to do here.
#3

[eluser]Emass[/eluser]
[quote author="jedd" date="1259430358"]
Are we allowed to know what the error is?
[/quote]
Certainly. I get a fatal error, Undefined class constant 'Page'.

So I now have a class named 'MY_Controller.php' placed in the libraries folder containing the following code.

Code:
class MY_Controller extends Controller {

    public $defaultPageid = 1;
    private $pageid;
    private $title;
    private $login = false;

    function __construct() {
        parent::Controller;

        $session = array(
                   'id'  => '1'
        );

        //$this->session->set_userdata($session);

        $this->setTitle();
    }


    function getLogin(){
        return $this->login;
    }
    function setTitle() {
        $this->title = 'Title...';
    }
    function getTitle() {
        return $this->title;
    }
    function setPageid($id) {
        $this->pageid = $id;
    }
    function getPageid() {
        return $this->pageid;
    }
}
$main = new MY_Controller;
Just how do I get this linked to the code in the 'page' class? My guess is that the $main = new MY_Controller; is not working, but how else do I get the page class to talk with 'My_Controller'?
I now get the following error: Fatal error: Undefined class constant 'Controller' in C:\xampp\htdocs\codeigniter\application\libraries\MY_Controller.php on line 10
Line 10 is the parent::Controller();

This is the code of Page, once more.
Code:
class Page extends Controller {

    function __construct() {
        parent::Controller();
        global $main;

        $main->setPageid($this->uri->segment(3, $main->defaultPageid));
    }

    function index() {
        global $main;
        $this->load->model('default_queries');

        $data['menu'] = $this->default_queries->getMenu();
        $data['content'] = $this->default_queries->getContent($main->getPageid());
        $data['title'] = $main->getTitle();
        $this->load->view('index', $data);
    }

    function cms() {
        global $main;
        //var_dump($main->getLogin());

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

        $data['menu'] = $this->default_queries->getMenu();
        $data['content'] = $this->default_queries->getContent($main->getPageid());
        $data['title'] = $main->getTitle();
        $this->load->view('index', $data);
    }
}
#4

[eluser]jedd[/eluser]
[quote author="Emass" date="1259434323"]
Just how do I get this linked to the code in the 'page' class?

This is the code of Page, once more.
Code:
class Page extends Controller {

    function __construct() {
        parent::Controller();
[/quote]

Change that code to this:

Code:
class Page extends MY_Controller {

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

I have recently written up a blurb about [url="/wiki/MY_Controller"]MY_Controller in the wiki[/url] if you want painful levels of detail.
#5

[eluser]Emass[/eluser]
Thanks. That tutorial is exactly what I'm looking for. I'm sure I'll find a way after reading that.

Thanks. Appreciate the help!




Theme © iAndrew 2016 - Forum software by © MyBB