Codeigniter constructor - Load database + set variables - php

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?

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;

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.

Related

how to handle situation where __construct() contains non existing property

I have a one PHP class in which there is below constructor function :
public function __construct()
{
$this->view_list = 'items';
parent::__construct();
}
It gives the error like
"The property view_list does not exist. Although not strictly required
by PHP, it is generally a best practice to declare properties
explicitly."
How can I handle this situation?
Thanks
You are getting the notice message because your class does not have a property called $view_list. PHP can handle this, but like the message indicates, it is considered best practice to create these beforehand. This makes the code easier to read. You can also set a default value for the variable, if you do not provide any variable value in the constructor.
class YourClassName extends SomeClass {
private $view_list;
public function __construct()
{
$this->view_list = 'items';
parent::__construct();
}
}
Or, setting the variable outside the constructor method, if the value is always the same:
class YourClassName extends SomeClass {
private $view_list = 'items';
public function __construct()
{
parent::__construct();
}
}

codeigniter $this->load->vars between controllers

hi found free source code based in codeigniter,
i have a controller in codeigniter some code is this:
class front extends main
{
function __construct()
{
parent::__construct();
$this->load_main_vars();
}
private function load_main_vars()
{
$vars['title'] = $this->option->get('title');
$vars['keywords'] = $this->option->get('keywords');
$vars['description'] = $this->option->get('description');
$this->db->order_by('category_name', 'ASC');
$this->db->where('category_active', 1);
$vars['categories'] = $this->db->get_where('categories', array('category_state' => 1))->result();
$this->load->vars($vars);
}
}
then i have another controller that extends front(code above):
class Site extends front
{
function __construct()
{
parent::__construct();
}
function index()
{
$vars['site'] = $this;
$this->view('site/index', $vars);
}
}
my question is why i have to use this $vars['site'] = $this; in Site controller to use vars array added in front controller and pass to view,
thanks
I don't think your code would work in CI at all and I'm going to only answer your question
A controller can't extend another controller,CI does not allow it, instead you need to create a My_controller.php in your application/core directory,add nessecary codes there and extend that class in your controller,now for start I'm going to give you an example based on your code
application/core/My_controller.php
class Main extends CI_controller {
// methods and vars....
}
class Front extends Main {
function __construct() {
parent::__construct();
$this->load_main_vars();
}
function load_main_vars() {
$vars = [];
//assigning some vars to $vars
$this->load->vars($vars);
}
}
so now you can create your controller:
application/controllers/Site.php
class Site extends Front {
function __construct() {
parent::__construct();
}
function index() {
$this->view('site/index'); // now all vars are accesable in your view
}
}
THis is the correct way to accomplish what you want in CI
Your example is far far away from secure application.
Dedicating $this to some variable that is going to output is NOT RECOMMENDED.
In that variable is stored many private data such are database credentials
and you won't to deal with allowing view file have those.
If I should make decision I would rate that application as least safe application in CodeIgniter world. Select that source code as dangerous insecure.
Never output $this
Never output get_instance()
Read first two rules again
Instead that:
In core controller make an array that you would have approach over.
It can be top level core class so you could have that array in children classes.
class Front extends Main
{
public $data = array();
function __construct()
{
parent::__construct();
$this->data['front_vars'] = $this->load_main_vars();
}
private function load_main_vars()
{
$vars['title'] = $this->option->get('title');
$vars['keywords'] = $this->option->get('keywords');
$vars['description'] = $this->option->get('description');
$this->db->order_by('category_name', 'ASC');
$this->db->where('category_active', 1);
$vars['categories'] = $this->db->get_where('categories', array('category_state' => 1))->result();
$this->load->vars($vars);
}
}
class Site extends front
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->view('site/index', $this->data['front_vars']);
}
}
In strict coding pay attention on capital cases of class names (and files) and on visibility of methods.

loading file on class construct

I'm working on a sql class, and trying to figure out how to retrieve data from an external php file, to be available in the entire class.
Im guesing i have to do something like this:
class sqlQuery {
protected $database = array();
function __construct(){
require_once (config.php);
}
}
class model extends sqlQuery {
function __construct() {
$this->connect($this->database['hostname'], $this->database['user'], $this->database['pass'], $this->database['database']);
}
}
The file might contain other information in the future, so I want it available to more then just the extended class.
Firstly you should call parent constructor in class model:
function __construct() {
parent::__construct();
// your other logic there
}
Then just change constructor of your parent class for filling $this->database array

Problem regarding use of constructors in PHP

My problem is in using Codeigniter custom library but I think it is not specific to that and more related to use of constructors in PHP.
I am trying to create a custom controller library in Codeigniter like this...
class MY_Controller extends Controller {
var $data = array();
function MY_Controller() {
parent::Controller();
$this->data['err'] = 'no';
$this->load->helper('utilities');
}
}
Now I create a codeigniter controller class like this...
class api_controller extends MY_Controller {
function get_data() {
$this->data['view'] = "data";
$this->load->view("data_view", $this->data);
}
function get_xml() {
$this->data['part'] = "xml";
$this->load->view("data_view", $this->data);
}
}
I want to ask that if the controller class extending the MY_Controller is instantiated when I access urls like api_controller/get_data and api_controller/get_xml, does the constructor of parent class always get called upon, i.e., MY_Controller() is always called.
Am I correct in inferring the following
get_data() called
-> $data => array ('err' => 'no', 'view' => 'data')
get_xml() called
-> $data => array ('err' => 'no', 'part' => 'xml')
Your code looks like it's using the PHP4 syntax for constructors. You should switch to the PHP5 syntax.
PHP4:
class MyClassName {
function MyClassName() {...} //constructor.
}
PHP5:
class MyClassName {
function __construct() {...} //constructor.
}
You can then call the constructor of a parent class by calling parent::__constructor(); from within the child class's __constructor() method:
class MyClassName extends SomeOtherClass {
function __construct() {
//...code here runs before the parent constructor.
parent::__construct();
//...code here runs after the parent constructor.
}
}
For PHP in general the parent constructor is not called default if the child has a constructor.
Constructors. It can be called using parent::_construct();
If you're using php 5+ you should go with the new recommended style of using function __construct() instead of the old style with a function named the same as a class.
As for CI-specific stuff I can't help you, sorry!
If you do not override __construct() in MY_Controller then Controller's __construct() will get run.
If you override it and then called parent::__construct() then it will run your own and the parent's.
So if you wanted it to run the parent's and your own you would do this
class MY_Controller extends Controller
{
var $data = array();
function __construct()
{
parent::__construct();
// Your code here
}
}
Yes, the parent constructor it is always called (you may want to rewrite them as __construct() however, thinking also at codeigniter 2.0 ).
If you really are in doubt toss in it an echo and see it for yourself.
Also the $this->data part is correct to me
You are right in your affirmations about the data array contents. In you code you wrote:
function MY_Controller() {
parent::Controller();
so the parents's class constructor is being called. There are lots of comments about PHP4 and PHP5 syntax, but basically everithing is ok.
In your question you wrote
that if the controller class extending the MY_Controller is instantiated
that is not correct. The instance is an object of class api_controller, calling the MY_Controller constructor is made using the same object. That is not the same, that is basic for polimorphism.
class Blog extends CI_Controller {
public function __construct()
{
parent::__construct();
// Your own constructor code
}
}

Creating a var from data created by another class

I am having problems creating a variable with data from another class. Here is what I am doing...
<?PHP
class Customers extends Controller {
private $foo = $this->session->userdata('foo');
}
You probably want something more like this:
class Customers extends Controller
{
private $foo;
public function __construct()
{
parent::__construct();
$this->foo = $this->session->userdata('foo');
}
}
It's hard to know for sure without knowing more about your project.
You can set it with constructor because you are inhering from parent class:
class Customers extends Controller {
private $foo = null;
function __construct(){
parent::__construct();
$this->foo = $this->session->userdata('foo');
}
}
This is not possible: $this doesn't exist at the moment when you define the class, and you can't call functions at all at this point at all.
You will need to assign $foo in the constructor, after $this->session has been initialized. (#konforce beat me to the example.)

Categories