Welcome Guest, Not a member yet? Register   Sign In
Newb Question on Exteneding Default Controller/Sessions
#1

[eluser]steven2[/eluser]
Ok some I'm very new to CodeIgniter, so please bear with me.

I'm fooling around with extending the default controller and I'm trying to figure out if I'm doing something wrong that would cause the constructor that I extended to not correctly be called.

So I auto-load the "session" library in the config file.

This is the code in "MY_Controller.php"
Code:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class AuthenticatedController extends Controller
{
  function __construct()
  {
    parent::Controller();
  

    $this->session->set_userdata('tester', 'testerval234ue');
    //return;
    /*
    if( ! $this->session->userdata('logged_in_value'))
    {
       redirect('login_controller');
    }*/
  }
}

This is the Code in my "Welcome Controller"

Code:
<?php

class Welcome extends AuthenticatedController {
    function __construct()
    {
        parent::Controller();    
        //$this->session->set_userdata('tester', '652');
        echo "'" . $this->session->userdata('tester') . "kk'";
        if ($this->session->userdata('tester') == false)
            echo "Does not exist in session.";
    }
    
    function index()
    {
        $this->load->view('welcome_message');
    }
}

When I load the welcome controller I don't get any error messages, but
Code:
$this->session->userdata('tester')

isn't being correctly set by the constructor in MY_Controller.php. Anyone know why this might be. I'm sure I'm doing something basic wrong. By the way I'm using PHP 5.2.
#2

[eluser]Dam1an[/eluser]
Hi, welcome to CI

In your welcome controller, in the construct, you're calling parent::Controller, and yet you don't have a Controller function in the parent, you have a __construct();

Also, I'm not sure if you can name the controller differant from the file name, but it's recommended to make the class name the same as the file name, so in this case, call the class MY_Controller and have your controller extend that
#3

[eluser]steven2[/eluser]
[quote author="Dam1an" date="1245558320"]Hi, welcome to CI

In your welcome controller, in the construct, you're calling parent::Controller, and yet you don't have a Controller function in the parent, you have a __construct();

Also, I'm not sure if you can name the controller differant from the file name, but it's recommended to make the class name the same as the file name, so in this case, call the class MY_Controller and have your controller extend that[/quote]

Thanks. I switched up the names of the Controller, I guess if I want to override the default controller I have to called the function "Controller". Makes perfect sense and now works.

Code:
class AuthenticatedController extends Controller
{
  function Controller()
  {
    parent::Controller();
  

    $this->session->set_userdata('tester', 'testerasdf');
    //return;
    /*
    if( ! $this->session->userdata('logged_in_value'))
    {
       redirect('login_controller');
    }*/
  }
}

And in Welcome Controller it is now:

Code:
class Welcome extends AuthenticatedController {
    function Welcome()
    {
        
        parent::Controller();    
        //$this->session->set_userdata('tester', '652');
        echo "'" . $this->session->userdata('tester') . "kk'";
        if ($this->session->userdata('tester') == false)
            echo "Does not exist in session.";
    }
    
    function index()
    {
        $this->load->view('welcome_message');
    }
}

Which makes much more sense to me, but why then does it say http://ellislab.com/codeigniter/user-gui...nstructors

that for PHP 5 you have to use function __construct() as your constructor?
#4

[eluser]Dam1an[/eluser]
That's not what I meant to do
Instead of calling the constructor in AuthenticatedController you should leave it as it was (__construct) and change the call from within the welcome controllers constructor to be parent::__construct()

As it is at the moment, the 'constructor' in the AuthenticatedController isn't actually a constructor, as it's not the same name as the class (PHP4 syntax) or __construct (PHP5 syntax)

Make sense?
#5

[eluser]steven2[/eluser]
[quote author="Dam1an" date="1245560120"]That's not what I meant to do
Instead of calling the constructor in AuthenticatedController you should leave it as it was (__construct) and change the call from within the welcome controllers constructor to be parent::__construct()

As it is at the moment, the 'constructor' in the AuthenticatedController isn't actually a constructor, as it's not the same name as the class (PHP4 syntax) or __construct (PHP5 syntax)

Make sense?[/quote]

Yes it makes sense and works perfectly. And thank you soo much for your help!

MY_Controller.php

Code:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class AuthenticatedController extends Controller
{
  function __construct()
  {
    parent::__construct();
  

    $this->session->set_userdata('tester', 'redbox');
    //return;
    /*
    if( ! $this->session->userdata('logged_in_value'))
    {
       redirect('login_controller');
    }*/
  }
}

Welcome Controller

Code:
class Welcome extends AuthenticatedController {
    function __construct()
    {
        
        parent::__construct();    
        //$this->session->set_userdata('tester', '652');
        echo "'" . $this->session->userdata('tester') . "kk'";
        if ($this->session->userdata('tester') == false)
            echo "Does not exist in session.";
    }

This works exactly as I expected it to, but seems to conflict with the userguide http://ellislab.com/codeigniter/user-gui...nstructors which says

Quote:In PHP 5, constructors use the following syntax:
<?php
Code:
class Blog extends Controller {

       function __construct()
       {
            parent::Controller();
       }
}
?>

This is probably where my confusion is coming from. Am I misreading what the user guide is saying?
#6

[eluser]Dam1an[/eluser]
In that example, you're creating a new controller with PHP5 syntax, but the parent that you're calling is written in PHP4 syntax, hence the difference in naming

Use what you prefer (I much rather __construct) Smile
#7

[eluser]steven2[/eluser]
[quote author="Dam1an" date="1245562254"]In that example, you're creating a new controller with PHP5 syntax, but the parent that you're calling is written in PHP4 syntax, hence the difference in naming

Use what you prefer (I much rather __construct) Smile[/quote]

Ok thanks so much I think I'm getting it. You can use either parent::Controller(); or parent::__construct; to call a PHP4 constructor, but if you create a PHP5 constructor you have to call it using __construct.

Thanks again for your help.
#8

[eluser]tomcode[/eluser]
Code:
// PHP4 syntax
class AuthenticatedController extends Controller
{
  function AuthenticatedController()
  {
    parent::Controller();
}
}
class Welcome extends AuthenticatedController {
  function Welcome()
  {
    parent::AuthenticatedController();
  }
}




Theme © iAndrew 2016 - Forum software by © MyBB